C#静态类构造函数 [英] C# static class constructor
本文介绍了C#静态类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以为静态类创建构造函数?
Is there a work around on how to create a constructor for static class?
在初始化类时,我需要加载一些数据,但是我需要一个
I need some data to be loaded when the class is initialized but I need one and only one object.
推荐答案
C#为此目的有一个静态构造函数。
C# has a static constructor for this purpose.
static class YourClass
{
static YourClass()
{
// perform initialization here
}
}
来自MSDN :
静态构造函数用于初始化任何静态数据,或用于
执行仅需要执行一次的特定操作。
在创建第一个实例或引用任何
静态成员之前会自动调用
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced
。
这篇关于C#静态类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文