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

自古美人都是妖i 提交于 2019-11-27 09:15:35

See ECMA 334 §17.4.5.1:

17.4.5.1 Static field initialization

The static field variable initializers of a class declaration 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 (§17.11) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class

Specifically: "execution of the static field initializers occurs immediately prior to executing that static constructor".

Your static MyClass aVar must be initialized before your static constructor executes (or, at least, it must appear that way). Without that static member, the static constructor should be called before any non-static constructors.

If you still want a MyClass singleton, you can put it in a container class and refer to it using that, e.g.:

public static class MyClassSingleton
{
    public static MyClass aVar = new MyClass();
}

It is caused by line public static MyClass aVar = new MyClass();.

In fact the aVar = new MyClass(); is prepend to the static contrstructor. So your static constructor:

static MyClass() {
    Console.WriteLine("Static");
}

is changed to:

static MyClass() {
    aVar = new MyClass(); // this will run instance contstructor and prints "Non-Static"
    Console.WriteLine("Static");
}

This public static MyClass aVar = new MyClass(); is part of your static constructor. If you look at it with reflector you will see the following:

static MyClass()
{
    aVar = new Program.MyClass();
    Console.WriteLine("Static");
}

So your result should be obvious now.

From MSDN Link:

A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

My Guess this is because of the static instantiation of the instance on the last line, but according to MSDN the static constructor should happen before the first instance is called.

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