什么时候在 C# 中调用静态构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1437352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:32:06  来源:igfitidea点击:

When is a static constructor called in C#?

c#static

提问by Jason

When I have class containing a static constructor, is that constructor called when the assembly containing the class is first loaded or when the first reference to that class is hit?

当我有一个包含静态构造函数的类时,该构造函数是在首次加载包含该类的程序集还是在对该类的第一个引用被命中时调用?

采纳答案by Jason

When the class is accessed for the first time.

第一次访问类时。

Static Constructors (C# Programming Guide)

静态构造函数(C# 编程指南)

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

静态构造函数用于初始化任何静态数据,或执行只需要执行一次的特定操作。在创建第一个实例或引用任何静态成员之前自动调用它。

回答by Greg Beech

It's not quite as simple as you might expect despite straightforward documentation. Jon Skeet's article http://csharpindepth.com/Articles/General/Beforefieldinit.aspxgoes into this question in details.

尽管有简单的文档,但它并不像您想象的那么简单。Jon Skeet 的文章http://csharpindepth.com/Articles/General/Beforefieldinit.aspx详细讨论了这个问题。

Summary:

概括:

Static constructor is guaranteed to be executed immediatelybefore the first reference to a member of that class - either creation of instance or own static method/property of class.

静态构造函数保证在第一次引用该类的成员之前立即执行- 创建实例或拥有类的静态方法/属性。

Note that static initilaizers (if there is no static constructor) guaranteed to be executed any timebefore first reference to particular field.

请注意,静态初始化程序(如果没有静态构造函数)保证在第一次引用特定字段之前的任何时间执行。

回答by Guffa

The static constructor is called before you use anything in the class, but exactly when that happens is up to the implementation.

在您使用类中的任何内容之前调用静态构造函数,但具体何时发生取决于实现。

It's guaranteed to be called before the first static member is accessed and before the first instance is created. If the class is never used, the static constructor is not guaranteed to be called at all.

保证在访问第一个静态成员之前和创建第一个实例之前调用它。如果从不使用该类,则根本不能保证调用静态构造函数。

回答by Andrew

In case static method is called from parent class, static constructor will not be called, althogh it is explicitly specified. Here is an example b constructor is not called if b.methoda() is called.

如果从父类调用静态方法,则不会调用静态构造函数,尽管它已明确指定。这是一个示例,如果调用 b.methoda(),则不会调用 b 构造函数。

static void Main(string[] args)
{
    b.methoda();
}

class a
{
    public static void methoda()
    {
        //using initialized method data
    }
}

class b : a
{
    static b()
    {
        //some initialization
    }
}    

回答by Etherman

There seems to be a gotcha with static constructors that is answered elsewhere but took a while to digest into a simple explanation. All the docs and explanations claim the static constructor/intializers are "guaranteed" to run before the first class is instantiated or the first static field is referenced. The gotcha comes in when you try to put a static singleton in the class that creates an instance of itself (chicken/egg). In this case the static constructor ends up being called after the instance constructor - and in my case the instance constructor contained code that relied on some static data.

似乎在其他地方回答了静态构造函数的问题,但需要一段时间才能消化成一个简单的解释。所有的文档和解释都声称静态构造函数/初始化器在第一个类被实例化或第一个静态字段被引用之前“保证”运行。当您尝试在创建自身实例(鸡/蛋)的类中放置一个静态单例时,问题就来了。在这种情况下,静态构造函数最终在实例构造函数之后被调用——在我的例子中,实例构造函数包含依赖于一些静态数据的代码。

Static constructor called after instance constructor?

在实例构造函数之后调用静态构造函数?

Static constructor can run after the non-static constructor. Is this a compiler bug?

静态构造函数可以在非静态构造函数之后运行。这是编译器错误吗?

(the answer for me was to put the singleton in a separate class or manually initialize the static data in the instance constructor before it is required)

(我的答案是将单例放在一个单独的类中,或者在需要之前手动初始化实例构造函数中的静态数据)