During my research on the best way to create Singleton in C #, I came across the following article where there is a brief mention of what in C ++
"The C ++ specification has left some ambiguity around initializing the order of static variables."
In the end, I looked through the question and found this and this . Where, basically, the point (as far as I understand) is that the order of initialization of static variables in C ++ is undefined. Ok, I guess itโs so good, but then I wanted to understand the following statement that the article later does
"Fortunately, the .NET Framework resolves this ambiguity through its handling of variable initialization."
So, I found this page where they say
The initializers of a static variable of a class field correspond to a sequence of assignments executed in the text order in which they are displayed in the class declaration.
and give an example
using System;
class Test
{
static void Main() {
Console.WriteLine("{0} {1}", B.Y, A.X);
}
public static int F(string s) {
Console.WriteLine(s);
return 1;
}
}
class A
{
static A() {}
public static int X = Test.F("Init A");
}
class B
{
static B() {}
public static int Y = Test.F("Init B");
}
the output must be:
Init B
Init A
1 1
"Because the rules for executing static constructors (as defined in Section 10.11) ensure that B static constructor (and therefore B static field initializers) must be executed before the static constructor and field initializers."
, , , , ( ). IE: , - - . , , , , ?
- ( , ) , , , , ?