статические конструкторы и BeforeFieldInit?
If a type has no static constructor, field initializers will execute just prior to the type being used— or anytime earlier at the whim of the runtime
Почему этот код:
void Main()
{
"-------start-------".Dump();
Test.EchoAndReturn("Hello");
"-------end-------".Dump();
}
class Test
{
public static string x = EchoAndReturn ("a");
public static string y = EchoAndReturn ("b");
public static string EchoAndReturn (string s)
{
Console.WriteLine (s);
return s;
}
}
дает:
-------start-------
a
b
Hello
-------end-------
пока этот код:
void Main()
{
"-------start-------".Dump();
var test=Test.x;
"-------end-------".Dump();
}
доходность
a
b
-------start-------
-------end-------
Получатель чего-тоa
а такжеb
понятно. но зачем иметь дело сstatic method
являетсяdifferent чемstatic field
.
Я имею в виду, почемуstart а такжеend строки в разных местах со статическими методами против статических полей? Я имею в виду - в обеих ситуациях он должен инициализировать эти поля ... так почему?
( I know I can add static ctor which make it to be the same - but Im asking about this particular situation. )
(p.s. Dump() is just like console.write)