【发布时间】:2013-12-25 10:28:53
【问题描述】:
我注意到一个非静态类可以有一个静态构造函数:
public class Thing
{
public Thing()
{
Console.WriteLine("non-static");
}
static Thing()
{
Console.WriteLine("static");
}
}
当您初始化Thing 的实例时,首先调用静态构造函数。
输出:
静态
非静态
这有什么需要?您是否使用它来初始化非静态类型实例上的静态字段?
在使用静态构造函数时有什么需要考虑的吗?
【问题讨论】:
-
您自己回答
to initialize static fields。阅读更多关于静态构造函数here。
标签: c# oop constructor static