C# 中关于类初始化时内部变量的执行顺序
class A
{
private static int i = 10;
static A()
{
Console.WriteLine("Init A");
}
public A()
{
i = 100;
Console.WriteLine(i.ToString());
}
public static void F()
{
Console.WriteLine("A.F");
}
public void F1()
{
Console.WriteLine("A.F1");
{
private static int i = 10;
static A()
{
Console.WriteLine("Init A");
}
public A()
{
i = 100;
Console.WriteLine(i.ToString());
}
public static void F()
{
Console.WriteLine("A.F");
}
public void F1()
{
Console.WriteLine("A.F1");
}
}
当这个类被A a=new A();的时候,首先执行的是 private static int i = 10;这个静态变量,接着执行 static A()
{
Console.WriteLine("Init A");
}静态构造函数,其次执行构造函数 public A()
{
i = 100;
Console.WriteLine(i.ToString());
}