(1) It is used to initialize static and read-only fields.
(2) You cannot add access modifiers when adding static keywords because static constructors are private.
(3) Static constructor of a class can be executed at most once in a given application domain: only the static constructor of the class is activated when an instance of the class is created or any static member of the referenced class is activated.
(4) Static constructors cannot be inherited and cannot be called directly.
(5) If the class contains the Main method used to start execution, the static constructor of the class will be executed before calling the Main method.
For any static field with an initial value, the initial values must be executed in the text order before executing the static constructor of this class.
(6) 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.
The following example code is used for further explanation:
/*************************************** ***********
* Static structure function training
* (1) ① ③ ...... Execution Order
* (2) output result: static ()
* Static B ()
* X = 1, y = 2
**************************************** ***********/
Using system;
Class
{
Public static int X;
Static A () // ④ return to ③ After execution
{
X = B .y + 1;
Console. writeline ("static ()");
}
}
Class B
{
Public static int y = a.x + 1; // ③ call the static member of,
// Go to the static constructor of A ---->
Static B () // ② If the static field with the Initial Value Setting item,
// When executing the static constructor of this class,
// Execute the initial values in the text order first.
// Go to the initial value setting item ---->
{
Console. WriteLine ("static B ()");
}
Static void Main () // ① program entry,
// If the class contains the Main method used to start execution,
// The static constructor of this class will be executed before the Main method is called.
// Go to the static constructor of B ---->
{
Console. writeline ("x = {0}, y = {1}", a.x, B .y); // ⑤ output result
Console. Readline ();
}
}
Supplement:
(1) Static members cannot be referenced through instances. However, you can reference it using the type name
(2) the class cannot use this to reference static methods or attribute accessors.
Http://hi.baidu.com/cheney_home/blog/item/5a83d01f997e500b304e15f9.html