永发信息网

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

答案:4  悬赏:80  手机版
解决时间 2021-01-19 20:28
  • 提问者网友:人生佛魔见
  • 2021-01-19 12:04
C#静态类的构造函数有用吗?
最佳答案
  • 五星知识达人网友:廢物販賣機
  • 2021-01-19 12:25
静态类不是不需要构造函数,而是不能定义普通的构造函数。
但是静态类可以定义静态构造函数。

“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;
}
}
全部回答
  • 1楼网友:深街酒徒
  • 2021-01-19 14:11
静态类不需要构造函数。直接调用就行了。
  • 2楼网友:蓝房子
  • 2021-01-19 13:09
静态类不需要构造函数
  • 3楼网友:行雁书
  • 2021-01-19 12:53
静态类应该是不需要构造函数的吧
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯