Prerequisite: Constructors in C#
Static constructors are used to initializing the static members of the class and implicitly called before the creation of the first instance of the class. Non-static constructors are used to initializing the non-static members of the class. Below are the differences between the Static Constructors and Non-Static Constructors.
- Declaration: Static constructors are declared using a static modifier explicitly while all other remaining constructors are non-static constructors. Non-static constructors can also be called as Instance Constructors as they need instance to get executed.
It is static constructor It is non-static constructor
- Calling: Static constructors are always called implicitly but the non-static constructors are called explicitly i.e by creating the instance of the class.
Example: In the above program, we have static constructor i.e static Geeks() which is called in the main method implicitly. See the output carefully, the code inside the static constructor is executing. But to call non-static constructor i.e Geeks(), you need to create an instance of the class, i.e obj1. So the creation of the object is to call the non-static constructor explicitly.
- Execution: Static constructor executes as soon as the execution of a class starts and it is the first block of code which runs under a class. But the non-static constructors executes only after the creation of the instance of the class. Each and every time the instance of the class is created, it will call the non-static constructor.
Static constructor Non-Static constructor Non-Static constructor
- Explanation: In the above program, there are two objects of Geeks() class is created i.e obj1 and obj2. obj1 and obj2 will call the non-static constructor twice because each and every time the instance of the class is created, it will call the non-static constructor. Although, in the Main method(entry point of a program), the first statement is “Geeks obj1 = new Geeks();” but as soon as the compiler found the Main Method control will transfer to class and the static block will be executed first. That’s why you can see in the output that Static Constructor is called first.
- Times of Execution: A static constructor will always execute once in the entire life cycle of a class. But a non-static constructor can execute zero time if no instance of the class is created and n times if the n instances are created.
Example: In the above program, you can see the static constructor is executing only once but the non-static constructor is executing 2 times as the two instances of the class is created. If you will not create an instance of the class then the non-static constructor will not execute.
- Initialization of fields: Static constructors are used to initialize the static fields and non-static constructors are used to initialize the non-static fields.
Static constructor Value of s is: 0 Non-Static constructor Value of ns is: 0
- Explanation: Here, both the static and non-static fields are initialized with default value. The default value of int type is zero. Static constructor will initialize only static fields. Here static field is s. While the non-static field(ns) is initialized by the non-static constructor.
- Parameters: We cannot pass any parameters to the static constructors because these are called implicitly and for passing parameters, we have to call it explicitly which is not possible. It will give runtime error as shown in below example. However, we can pass the parameters to the non-static constructors.
prog.cs(18, 16): error CS0132: `Geeks.Geeks(int)’: The static constructor must be parameterless
- Overloading: Non-static constructors can be overloaded but not the static constructors. Overloading is done on the parameters criteria. So if you cannot pass the parameters to the Static constructors then we can’t overload it.
- Cases in which the constructor will be implicit: Every class except the static class(which contains only static members) always contains an implicit constructor if the user is not defining any explicit constructor. If the class contains any static fields then the static constructors are defined implicitly.