2 <clause number="17.4.5.1" title="Static field initialization">
3 <paragraph>The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor (<hyperlink>17.11</hyperlink>) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class. <example>[Example: The example <code_example><![CDATA[
8 Console.WriteLine("{0} {1}", B.Y, A.X);
10 public static int f(string s) {
17 public static int X = Test.f("Init A");
21 public static int Y = Test.f("Init B");
23 ]]></code_example>might produce either the output: <code_example><![CDATA[
27 ]]></code_example>or the output: <code_example><![CDATA[
31 ]]></code_example>because the execution of X's initializer and Y's initializer could occur in either order; they are only constrained to occur before the references to those fields. However, in the example: <code_example><![CDATA[
35 Console.WriteLine("{0} {1}", B.Y, A.X);
37 public static int f(string s) {
45 public static int X = Test.f("Init A");
50 public static int Y = Test.f("Init B");
52 ]]></code_example>the output must be: <code_example><![CDATA[
56 ]]></code_example>because the rules for when static constructors execute provide that B's static constructor (and hence B's static field initializers) must run before A's static constructor and field initializers. end example]</example> </paragraph>