1. When you create the first instance of a type, the order of operations is:
1. Static variable set to 0
2. Execute static variable initializer
3. Executing a static constructor for a base class
4. Execute static constructors
5. Instance variable set to 0
6. Execute 衯 variable initializer
7. Execute the appropriate instance constructor in the base class
8. Execute instance Constructors
A typical example:
Public classa{ Public Static ReadOnly intx; StaticA () {x= B.y +1; }} classb{ Public Static inty = a.x +1; Static voidMain (string[] args) {Console.WriteLine ("X:{0},y:{1}. ", a.x, y); Console.ReadLine (); }}
View Code
The default initial value of a global variable for a simple value type is 0. Because the entry function is in class B, the program first loads B, because Y is a static member of B, the value of Y is computed when the class is loaded, but the value of Y is found to be obtained by assigning a value, and the value of x is immediately computed. X is initialized by the static constructor of Class A, and references the y of B, at which point y is still 0 (because the assignment statement of Y is not finished, so its value is the default), get x=0+1=1, continue to be worth to Y y=x+1=2.
For details, refer to C # static constructors (note the comments later in this article)
C # Trivia