Difference between Static Constructors and Non-Static Constructors in C#

In C#, a constructor is a class method automatically called when an object is created. These constructors are used to initialize the objects with some values. Constructors will have the same name as the class, and they will not have any return type.

There are static constructors, non-static constructors, default constructors, and parameterized constructors. If a constructor takes the parameters, it is called a parameterized constructor, whereas default constructors will not have the parameters.

If a constructor initializes static members, it is called a static constructor, whereas a non-static constructor will initialize the instance members.

Static Constructors:

Static Constructors are used to initialize the static members of a class. It does not take any parameters, and its declaration does not include an access modifier or the "static" keyword. The static constructor will be executed only once. Typically, it occurs before any static members are accessed or static methods are called in the class. A static constructor cannot have an access modifier like public or private. The runtime automatically calls it.

Syntax:

It has the following syntax:

Example:

Let us take a C# program to illustrate the static constructors.

Output:

Difference between Static Constructors and Non-Static Constructors in C#

Explanation:

The C# program defines a class named MyClass with a static variable staticVariable and a static constructor (static MyClass()) that initializes this variable to 100. The program includes a static method PrintStaticVariable() in MyClass that prints the value of the static variable. In the Main method of the Program class, the static method PrintStaticVariable() is called twice on the MyClass type, resulting in the execution of the static constructor only once. The program output displays the message "Static constructor is called" once, confirming the static constructor's single execution, and then prints the static variable value twice, indicating that the static variable retains its value across multiple calls to the static method.

Non-static constructors:

Non-static constructors are also called instance constructors. Instance constructors are used to initialize instance members of a class. A non-static constructor has the name of the class and can take parameters. It is executed whenever an instance of the class is created using the "new" keyword. Non-static constructors can have access modifiers like public or private, allowing control over the visibility of the constructor.

Syntax:

It has the following syntax:

Example:

Let us take a C# program to illustrate the non-static constructors.

Output:

Difference between Static Constructors and Non-Static Constructors in C#

Explanation:

The C# program defines a class named MyClass with an instance variable instanceVariable and a non-static constructor (public MyClass(int value)) that initializes this variable with a specified value. In the Main method of the Program class, two instances of MyClass are created with different values using the new keyword. The non-static constructor is called for each instance, displaying the message "Non-static constructor is called". Subsequently, the program invokes the PrintInstanceVariable() method for each object, printing the respective values of their instance variables. In essence, the program demonstrates the creation of instances with unique states through the non-static constructor, highlighting the encapsulation of instance-specific behaviour within the class.

Differences between the static and non-static constructors:

Difference between Static Constructors and Non-Static Constructors in C#

There are several differences between the static and non-constructors in C#. Some main differences are as follows:

Static constructorsNon-Static Constructors
These are associated with the class itself.These are associated with the instances of the class
These are executed only once, before any static members are accessed or methods are called.These are executed each time an instance of the class of created using the "new" keyword.
Static constructors are declared without an access modifier and the "static" keyword.These are declared with optional access modifiers (public, private, etc.).
These constructors cannot take parameters.Non-static constructors can take parameters.
These are automatically called by the runtime.These are explicitly called using the "new" keyword when creating an object.
These are used for one-time initialization of static members.These are used for initializing instance (non-static) members.
Static constructors can access only static members and call other static methods only.Non-static constructors can access both static and instance members including other instance methods.
These can't be overloaded or explicitly called by user code.These can be overloaded, allowing multiple constructors with different parameters.
Static constructors can't be used for late binding.These constructors are used for late binding, allowing multiple constructors with different parameters.
Initialization order is determined by the order in which the static members are declared in the code.Initialization order is determined by the order in which the non-static members are executed during construction.

Conclusion:

The static constructors are associated with the class itself, and are used for one-time initialization of static members. In contrast, the non-static constructors are associated with instances of the class, and are used to initialize instance members when an object is created.






Latest Courses