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

Posted by Ryan Ische on Stack Overflow See other posts from Stack Overflow or by Ryan Ische
Published on 2010-04-13T12:59:57Z Indexed on 2010/04/13 13:02 UTC
Read the original article Hit count: 344

Filed under:
|

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" );
            }
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about static-constructor