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

后端 未结 4 481
轮回少年
轮回少年 2020-12-01 21:53

The output from the following program is:

Non-Static
Static
Non-Static

Is this a compiler bug? I

4条回答
  •  长情又很酷
    2020-12-01 22:08

    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.

提交回复
热议问题