Why isn't the static constructor of the parent class called when invoking a method on a nested class?

*爱你&永不变心* 提交于 2019-12-23 10:48:15

问题


Given the following code, why isn't the static constructor of "Outer" called after the first line of "Main"?

namespace StaticTester
{
    class Program
    {
        static void Main( string[] args )
        {
            Outer.Inner.Go();
            Console.WriteLine();

            Outer.Go();

            Console.ReadLine();
        }
    }

    public static partial class Outer
    {
        static Outer()
        {
            Console.Write( "In Outer's static constructor\n" );
        }

        public static void Go()
        {
            Console.Write( "Outer Go\n" );
        }

        public static class Inner
        {
            static Inner()
            {
                Console.Write( "In Inner's static constructor\n" );
            }

            public static void Go()
            {
                Console.Write( "Inner Go\n" );
            }
        }
    }
}

回答1:


In the case of nested classes, if the nested class never refers to the static members of it's outer scope, the compiler (and CLR) are not required to call the static constructor of that outer class.

If you want to force the static constructor to run, just add code to the inner type that performs a read of a field or property of the outer type.

You can read more about the lazy initialization semantics of C# on Jon Skeet's blog - it's quite good. You could also check out his book - C# In Depth, it covers these topics as well ... in depth.




回答2:


Your question is answered by section 10.12 of the specification, which states:

The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

• An instance of the class type is created.

• Any of the static members of the class type are referenced.

Since you've done neither of those two things, the ctor is not executed.




回答3:


Outer.Inner is just referring to a type, it's not actually invoking anything on "Outer".




回答4:


A static initializer only gets executed when the containing class is used for the first time.

By calling Outer.Inner, you are not using Outer at all, since Outer.Inner is a different type than Outer. So, the static initializer in Outer will not run.



来源:https://stackoverflow.com/questions/2629669/why-isnt-the-static-constructor-of-the-parent-class-called-when-invoking-a-meth

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