Search code examples
c#constructorstatic-constructor

will the compiler add default static constructor in c#


In general the compiler adds the default constructor when we dont declare any constructor explicitly. In the same way does it also allocate a static constructor by default. If no, why? If yes, why? and in which situation?


Solution

  • If you don’t provide a constructor for your class, C# creates one by default that instantiates the object and sets member variables to the default values as listed in the Default Values Table. Constructor without any parameters is called a default constructor. In other words, this type of constructor does not take parameters. The drawback of a default constructor is that every instance of the class will be initialized to the same values and it is not possible to initialize each instance of the class to different values.

    The default constructor initializes:

    All numeric fields in the class to zero. All string and object fields to null.