Why the default constructor executed before the static constructor?

浪尽此生 提交于 2019-12-11 07:45:57

问题


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

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


回答1:


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.

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

  • 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?



回答2:


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. 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. 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.



来源:https://stackoverflow.com/questions/53018370/why-the-default-constructor-executed-before-the-static-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!