C#静态类的构造函数有用吗?

2024-12-25 08:44:14
推荐回答(4个)
回答1:

静态类不是不需要构造函数,而是不能定义普通的构造函数。
但是静态类可以定义静态构造函数。

“Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor; however, they can have a static constructor. For more information, see Static Constructors (C# Programming Guide). ”这是MSDN的官方说明。

举例:
static StaticClassSample {
static int Count = 0;
//public StaticClassSample () {} wrong
static StaticClassSample () {
Count = 0;
}
}

回答2:

静态类不需要构造函数。直接调用就行了。

回答3:

静态类不需要构造函数

回答4:

静态类应该是不需要构造函数的吧