• The static constructor neither has an access modifier nor a parameter.
• If a static constructor is not compiled and the class contains a static field with an initial value, the compiler automatically generates the default static constructor.
• When creating the first class instance or any static member is referenced ,. NET will automatically call the static constructor to initialize the class. That is to say, we cannot directly call the static constructor, so we cannot control when to execute the static constructor.
• If the class contains the Main method used to start execution, the static constructor of the class will be executed before the Main method is called.
• If the static field in the class carries initialization, the initialization statement of the static field runs before the static constructor.
• A class can have only one static constructor.
• A non-parameter constructor can coexist with a static constructor. Although the parameter list is the same, one belongs to the class and the other belongs to the instance, so there is no conflict.
• Run only once at most.
• Static constructors cannot be inherited.
• The sample code is as follows:
The running result is:
Static ()
Static B ()
X = 1, Y = 2