简体   繁体   English

为什么默认构造函数在静态构造函数之前执行?

[英]Why the default constructor executed before the static constructor?

I am wondering why my static constructor is outputting default constructor Static Constructor , and not the other way around Static Constructor and Default constructor or just Default constructor . 我想知道为什么我的静态构造函数会输出default constructor Static Constructor ,而不是围绕“ Static Constructor and Default constructor或者只是“ Default constructor When I use a static constructor, it should execute the static constructor first. 当我使用静态构造函数时,它应该首先执行静态构造函数。 However, from the code below, 但是,从下面的代码中,

The First question: why is the default constructor is called before the static constructor? 第一个问题:为什么在静态构造函数之前调用默认构造函数?

class Program
{
    static void Main(string[] args)
    {
        var test = Single.S;
    }
    class Single{
        static readonly Single s = new Single();

        public static Single S{
            get { return s; }
        }
        private Single(){
            Console.WriteLine("Default");

        }
        static Single(){
            Console.WriteLine("staic");
        }
    }
}

The Second question: How come the Static Single constructor is being called as well? 第二个问题:为什么还要调用静态单一构造函数?

Depending on Microsoft 取决于微软

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. 在创建第一个实例或引用任何静态成员之前,将自动调用它。

 class SimpleClass { // Static variable that must be initialized at run time. static readonly long baseline; // Static constructor is called at most one time, before any // instance constructor is invoked or member is accessed. static SimpleClass() { baseline = DateTime.Now.Ticks; } } 

In this case, the static constructor will be called before the default constructor 在这种情况下,将在默认构造函数之前调用静态构造函数


but in this line, 但在这一行中

static readonly Single s = new Single();

you are using "static field initialization" 您正在使用“静态字段初始化”

The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. 类的静态字段变量初始值设定项对应于以文本顺序执行的分配序列,这些赋值序列出现在类声明中。 If a static constructor exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. 如果类中存在静态构造函数,则在执行该静态构造函数之前立即执行静态字段初始化程序。

in this case, static field initialization will be completed before the constructor is called, 在这种情况下,静态字段初始化将在调用构造函数之前完成,

then the static constructor is run before your default constructor, 然后在您的默认构造函数之前运行静态构造函数,

but when it runs, it's using new Single() , so passing through your non-static constructor path. 但是当它运行时,它使用的是new Single() ,因此要通过您的非静态构造函数路径。

This causes to call the default constructor before the static constructor. 这将导致在静态构造函数之前调用默认构造函数。

Finally , you can try this and you will see it will execute the static constructor before the default constructor 最后 ,您可以尝试一下,您会看到它将在默认构造函数之前执行静态构造函数

private class Single
{
    private static readonly Single s;
    static Single()
    {
        Console.WriteLine("static");
        s = new Single();
    }
}

References 参考

The first thing that happens here when loading the class is 加载类时在这里发生的第一件事是

static Single s = new Single();

C# executes static initializers like this in the order they are seen. C#按看到的顺序执行此类静态初始化程序。 I am not sure if this ordering point also applies to the static constructor but from your test it seems that the static initializer does occur before the static constructor. 我不确定此排序点是否也适用于静态构造函数,但是从您的测试看来,静态初始化程序确实发生在静态构造函数之前。

In any case, in order to assign to static Single s, one must first construct new Single(), which means invoking the non-static constructor. 无论如何,要分配给静态Single,必须首先构造新的Single(),这意味着调用非静态构造函数。 I am not sure what you would get if you read from Single.s in the non-static constructor but I would expect either null or an exception. 我不确定如果从非静态构造函数中读取Single.s会得到什么,但是我期望null或异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM