为什么这段代码:
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
的顺序是可以理解的,但为什么处理静态方法
与静态字段
不同呢?我的意思是为什么静态方法和静态字段的起始和结束行不同?我是说,在这两种情况下,他都必须初始化这些字段...那么为什么?
(我知道我可以添加静态构造函数使它们相同 - 但我正在询问这个特定的情况。)
(附注:Dump()就像console.write)