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

StackOverflow https://stackoverflow.com/questions/2629669

  •  26-09-2019
  •  | 
  •  

Question

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" );
            }
        }
    }
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top