为什么不首先命中静态构造函数?
Why isn't the static constructor hit first?
我有下面的代码,我只有两个简单的问题,这些问题在下面的程序行为中是正常的,不幸的是我没有看到:
- 任何 class 中的静态构造函数应该是第一个在静态字段被命中后立即被命中的构造函数。然后只有实例构造函数。但我看到的是,调试首先转到 public 构造函数。可悲的事情。为什么?
虽然:程序的输出是:
This is Staticcc...
1
...这是正确的。
- 我在静态构造函数的开头保留了一个断点,但是当我调试时它只显示静态构造函数的结束大括号中的断点。为什么?
示例代码:
public sealed class B : A, C
{
public int? m = 0;
public B()
{
m = 1;
}
private B(int a, int b)
{
m = 2;
}
protected B(int x, int y, int z)
{
m = 3;
}
static B()
{
Console.WriteLine("THis is staticcc");
}
public static B b = new B();
public static B BC
{
get
{
return b;
}
}
static void Main()
{
Console.WriteLine(B.BC.m);
Console.ReadKey();
}
}
public interface C
{
}
public class A
{
//private A()
//{
//}
}
这是问题所在:
public static B b = new B();
在执行静态构造函数之前执行静态字段初始值设定项。来自 C# 规范第 10.5.5.1 节:
If a static constructor (10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor.
您的静态字段初始值设定项调用您的实例构造函数,因此实例构造函数是首先要执行的。
Although: the output of the program is This is Staticcc... then 1...Which is correct.
是的,因为所有初始化都是在 Main
中评估 B.BC.m
的一部分。
如果将 Console.WriteLine("Instance Constructor");
添加到构造函数中,您将看到:
Instance Constructor
THis is staticcc
1
如果这没有帮助,请将 Main
想象成这样:
int tmp = B.BC.m; // This prints initialization bits
Console.WriteLine(tmp); // This prints 1
那是因为这一行 运行 在 static
构造函数之前:
public static B b = new B();
你在 static
构造函数中的第一行(你看不到但实际上存在的部分)实际上是调用 B
的构造函数。这就是您看不到 static
构造函数首先被命中的原因。
如果你这样写,你会看到 static
构造函数首先命中:
static B()
{
Console.WriteLine("THis is staticcc");
b = new B();
}
public static B b;
我有下面的代码,我只有两个简单的问题,这些问题在下面的程序行为中是正常的,不幸的是我没有看到:
- 任何 class 中的静态构造函数应该是第一个在静态字段被命中后立即被命中的构造函数。然后只有实例构造函数。但我看到的是,调试首先转到 public 构造函数。可悲的事情。为什么?
虽然:程序的输出是:
This is Staticcc...
1
...这是正确的。
- 我在静态构造函数的开头保留了一个断点,但是当我调试时它只显示静态构造函数的结束大括号中的断点。为什么?
示例代码:
public sealed class B : A, C
{
public int? m = 0;
public B()
{
m = 1;
}
private B(int a, int b)
{
m = 2;
}
protected B(int x, int y, int z)
{
m = 3;
}
static B()
{
Console.WriteLine("THis is staticcc");
}
public static B b = new B();
public static B BC
{
get
{
return b;
}
}
static void Main()
{
Console.WriteLine(B.BC.m);
Console.ReadKey();
}
}
public interface C
{
}
public class A
{
//private A()
//{
//}
}
这是问题所在:
public static B b = new B();
在执行静态构造函数之前执行静态字段初始值设定项。来自 C# 规范第 10.5.5.1 节:
If a static constructor (10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor.
您的静态字段初始值设定项调用您的实例构造函数,因此实例构造函数是首先要执行的。
Although: the output of the program is This is Staticcc... then 1...Which is correct.
是的,因为所有初始化都是在 Main
中评估 B.BC.m
的一部分。
如果将 Console.WriteLine("Instance Constructor");
添加到构造函数中,您将看到:
Instance Constructor
THis is staticcc
1
如果这没有帮助,请将 Main
想象成这样:
int tmp = B.BC.m; // This prints initialization bits
Console.WriteLine(tmp); // This prints 1
那是因为这一行 运行 在 static
构造函数之前:
public static B b = new B();
你在 static
构造函数中的第一行(你看不到但实际上存在的部分)实际上是调用 B
的构造函数。这就是您看不到 static
构造函数首先被命中的原因。
如果你这样写,你会看到 static
构造函数首先命中:
static B()
{
Console.WriteLine("THis is staticcc");
b = new B();
}
public static B b;