views:

111

answers:

4

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" );
            }
        }
    }
}
+3  A: 

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

Ryan Ische
I've put this here so other don't ponder this for as long as I did.
Ryan Ische
A: 

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.

Eric Eijkelenboom
Ah, didn't see your comment there:)
Eric Eijkelenboom
+3  A: 

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.

LBushkin
That's funny, I pulled out my copy of C# In Depth to help figure out why.
Ryan Ische
+2  A: 

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.

Eric Lippert