为什么默认构造函数在静态构造函数之前执行? [英] Why the default constructor executed before the static constructor?

查看:263
本文介绍了为什么默认构造函数在静态构造函数之前执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我的静态构造函数输出的是默认构造函数静态构造器,而不是相反的静态构造器和默认构造函数或仅默认构造函数。当我使用静态构造函数时,它应该首先执行静态构造函数。但是,从下面的代码中,

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. 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");
        }
    }
}

第二个问题:还如何调用Static Single构造函数?

The Second question: How come the Static Single constructor is being called as well?

推荐答案

取决于> Microsoft


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

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

但在这一行

静态只读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,

然后在默认构造函数

但是当它运行时,它使用的是 new Single(),因此要通过您的非静态构造函数路径。

but when it runs, it's using new Single(), so passing through your non-static constructor path.

这会导致在静态构造函数之前调用默认构造函数。

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();
    }
}






参考文献

  • C# and beforefieldinit
  • When is a static constructor called in C#?
  • How does static field initialization work in C#?
  • Static constructor called after instance constructor?
  • Static constructor can run after the non-static constructor. Is this a compiler bug?

这篇关于为什么默认构造函数在静态构造函数之前执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆