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: 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: 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:There are several differences between the static and non-constructors in C#. Some main differences are as follows:
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. Next TopicDouble.IsInfinity() Method in C# |