Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 17 Classesprevious at this levelnext at this level 17.11 Static constructors Paragraph 11 A static constructor is a member that implements the actions required to initialize a class. 2 Static constructors are declared using static-constructor-declarations: static-constructor-declaration : attributesopt static-constructor-modifiers identifier ( ) static-constructor-body static-constructor-modifiers : externopt static static externopt static-constructor-body : block ; Paragraph 21 A static-constructor-declaration may include a set of attributes (§24) and an extern modifier (§17.5.7). Paragraph 31 The identifier of a static-constructor-declaration must name the class in which the static constructor is declared. 2 If any other name is specified, a compile-time error occurs. Paragraph 41 When a static constructor declaration includes an extern modifier, the static constructor is said to be an external static constructor. 2 Because an external static constructor declaration provides no actual implementation, its static-constructor-body consists of a semicolon. 3 For all other static constructor declarations, the static-constructor-body consists of a block, which specifies the statements to execute in order to initialize the class. 4 This corresponds exactly to the method-body of a static method with a void return type (§17.5.8). Paragraph 51 Static constructors are not inherited, and cannot be called directly. Paragraph 61 The static constructor for a class executes at most once in a given application domain. 2 The execution of a static constructor is triggered by the first of the following events to occur within an application domain: Paragraph 71 If a class contains the Main method (§10.1) in which execution begins, the static constructor for that class executes before the Main method is called. 2 If a class contains any static fields with initializers, those initializers are executed in textual order immediately prior to executing the static constructor. [Example: The example
using System;  
class Test  
{  
   static void Main() {  
      A.F();  
      B.F();  
   }  
}  
class A  
{  
   static A() {  
      Console.WriteLine("Init A");  
   }  
   public static void F() {  
      Console.WriteLine("A.F");  
   }  
}  
class B  
{  
   static B() {  
      Console.WriteLine("Init B");  
   }  
   public static void F() {  
      Console.WriteLine("B.F");  
   }  
}  
must produce the output:
Init A  
A.F  
Init B  
B.F  
because the execution of A's static constructor is triggered by the call to A.F, and the execution of B's static constructor is triggered by the call to B.F. end example]
3 It is possible to construct circular dependencies that allow static fields with variable initializers to be observed in their default value state. [Example: The example
using System;  
class A  
{  
   public static int X;  
   static A() { X = B.Y + 1;}  
}  
class B  
{  
   public static int Y = A.X + 1;  
   static B() {}  
   static void Main() {  
      Console.WriteLine("X = {0}, Y = {1}", A.X, B.Y);  
   }  
}  
produces the output
X = 1, Y = 2  
To execute the Main method, the system first runs the initializer for B.Y, prior to class B's static constructor. Y's initializer causes A's static constructor to be run because the value of A.X is referenced. The static constructor of A in turn proceeds to compute the value of X, and in doing so fetches the default value of Y, which is zero. A.X is thus initialized to 1. The process of running A's static field initializers and static constructor then completes, returning to the calculation of the initial value of Y, the result of which becomes 2. end example]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS