The following code:
static void Main(string[] args)
{
Console.WriteLine("0");
string h = Foo.X;
Console.WriteLine("2");
}
public static class Foo
{
public static string X = ((Func<string, string>)delegate(string g)
{
Console.WriteLine(g);
return (g);
})("_aaa");
static Foo()
{
Console.WriteLine("ctor");
}
}
Will print:
0
_aaa
ctor
2
I know about the beforefieldinit
behavior (with/without static constructor etc.).
The thing which I don't understand is why the ctor
(in the output) is after _aaa
?
It doesn't make any sense, what if I want to initialize variables in the constructor?
Question
Why does the initialization of X
is before the ctor
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…