2
Reply

Difference between type constructor and instance constructor? What is static constructor, when it will be fired? And what is its use?

18y
16.7k
0
Reply

    Go through this- https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/instance-constructors

    The Common Language Runtime Object Model provides two special methods, the instance constructor and the class constructor. This class constructor method is also known as type constructor or type initializer.

    Instance constructor is executed when a new instance of type is created and the class constructor is executed after the type is loaded and before any one of the type members is accessed. We have a perfect setup for field initialization since the class constructors are static and can deal with static members of the type only and instance constructors take care of instance members only.

    Class constructor, or type initializer, is the method specific to a type as a whole that run after the type is loaded and before any of the type's members are accessed. Class constructors are used for static field initialization. Only one class constructor per type is permitted, and it cannot use the vararg (variable argument) calling convention. Normally, class constructors are never called from the IL code. If a type has a class constructor, this constructor is executed automatically after the type is loaded. However, a class constructor, like any other static method, can be called explicitly. As a result of such a call, the global fields of the type are reset to their initial values. Calling class constructor explicitly does not lead to type reloading.

    Instance constructors, unlike class constructors, are specific to an instance of a type and are used to initialize both static and instance fields of the type. Functionally, instance constructors in IL are a direct analog of C++ constructors. Instance constructors can have parameters but must return void. Like class constructors, instance constructors cannot use the vararg calling convention. In the current release of the common language runtime, instance constructors are not allowed to be a virtual type. A type can have multiple instance constructors, but they must have different parameter lists because the name and the return type (void) are fixed. Usually, instance constructors are called during a new type instance is created. An instance constructor can be called explicitly and calling it resets the fields of the type instance and does not create a new instance. You should be aware of explicitly calling constructors especially when you have reference type members; this is just to avoid multiple type instantiations. Calling the class and instance constructors explicitly is possible, but it makes the code unverifiable. This limitation is imposed on the constructors of reference types (classes) only and does not concern those of the value types. But wait, the instance constructor can explicitly called within the instance of constructor of the class's direct descendent or child.

    Source: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=116

    18y
    0