C# | Difference between Static Constructors and Non-Static Constructors
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.
Example:
// C# Program to demonstrate// how to declare the static// constructor and non-static// constructorusingSystem;classGeeks {// static variablestaticints;// non-static variableintns;// declaration of// static constructorstaticGeeks(){Console.WriteLine("It is static constructor");}// declaration of// non-static constructorGeeks(){Console.WriteLine("It is non-static constructor");}// Main MethodstaticvoidMain(string[] args){// static constructor will call implicitly// as soon as the class start to execute// the first block of code to execute// will be static constructor// calling non-static constructorGeeks obj1 =newGeeks();}}chevron_rightfilter_noneOutput:
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 ouput 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.
Example:
// C# Program to demonstrate// the execution of static// constructor and non-static// constructorusingSystem;classGeeks {// declaration of// static constructorstaticGeeks(){Console.WriteLine("Static constructor");}// declaration of// non-static constructorGeeks(){Console.WriteLine("Non-Static constructor");}// Main MethodstaticvoidMain(string[] args){// static constructor will call implicitly// as soon as the class start to execute// the first block of code to execute// inside the class will be static// constructor// calling non-static constructor// here we are calling non-static// constructor twice as we are// creating two objectsGeeks obj1 =newGeeks();Geeks obj2 =newGeeks();}}chevron_rightfilter_noneOutput:
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.
Example:
// C# Program to demonstrate// initialization of fields// by using static constructor// and non-static constructorusingSystem;classGeeks {// static variablestaticints;// non-static variableintns;// declaration of// static constructorstaticGeeks(){Console.WriteLine("Static constructor");}// declaration of// non-static constructorGeeks(){Console.WriteLine("Non-Static constructor");}// Main MethodstaticvoidMain(string[] args){// static fields can// be accessed directlyConsole.WriteLine("Value of s is: "+ s);// calling non-static constructorGeeks obj1 =newGeeks();// printing the value// of non-static fieldConsole.WriteLine("Value of ns is: "+ obj1.ns);}}chevron_rightfilter_noneOutput:
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.
Example:
// C# Program to demonstrate// the passing of paramters// to constructorusingSystem;classGeeks {// static variablestaticints;// non-static variableintns;// declaration of// static constructor// and passing parameter// to static constructorstaticGeeks(intk){k = s;Console.WriteLine("Static constructor & K = "+ k);}// declaration of// non-static constructorGeeks(){Console.WriteLine("Non-Static constructor");}// Main MethodstaticvoidMain(string[] args){}}chevron_rightfilter_noneRuntime Error:
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.
Recommended Posts:
- C# | Constructors
- Private Constructors in C#
- C# | Inheritance in Constructors
- Static keyword in C#
- C# | Static Class
- Difference between var and dynamic in C#
- Difference between Ref and Out keywords in C#
- Difference between Class and Structure in C#
- Difference between SortedList and SortedDictionary in C#
- Difference between Hashtable and Dictionary in C#
- Difference between Boxing and Unboxing in C#
- Difference between Managed and Unmanaged code in .NET
- Difference between Abstract Class and Interface in C#
- Difference between readonly and const keyword in C#
- Difference between Method Overriding and Method Hiding in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



