Initialization order of a static field with partial classes

Is there a way to force the initialization order of a static field in partial classes? Let's say in HelloWorld1.cs I have:

partial class HelloWorld
{
  static readonly string[] a = new[] { "Hello World" };
}

Elsewhere in HelloWorld2.cs I have:

partial class HelloWorld
{
  static readonly string b = a[0];
}

If a is initialized to b, this is normal, but if b is initialized to a, then it returns a. A healthy way is probably to use a static constructor, but I'm curious if there is a way to force or predict the initialization order when the field classes are in different files of the same partial class.

+4
source share
2 answers

When fields are present in one file, the text order determines the execution of their initialization:

10.5.5.1 -

, , . (Β§ 10.12), . .

, , undefined:

10.2.6 -

#, . , , undefined.

#.

+10

MSDN:

, . ( 10.11) , . , , .

+1

All Articles