Why this static constructor is not getting called?

Today my static initializer was not being called. It turns out static initializers are not called prior to accessing const members of a class.

Since const values are known at compile time this makes sense, but it means that the documentation which states "It is called automatically before ... any static members are referenced" is technically incorrect, at least when combined with @JonSkeet's assertion that "All constants declarations are implicitly static".

This program demonstrates the problem:

using System;

static class Program
{
    public static void Main()
    {
        Console.WriteLine("Constant={0}", Problem.Constant);
        Console.WriteLine("ReadOnly={0}", Problem.ReadOnly);
        Console.WriteLine("Field={0}", Problem.Field);
        Console.WriteLine("Property={0}", Problem.Property);
    }

    private static class Problem
    {
        public const int Constant = 1;
        public static readonly int ReadOnly = 2;
        public static int Field = 3;
        private static int mProperty = 4;
        public static int Property { get { return mProperty; } }

        static Problem()
        {
            Console.WriteLine("Problem: static initializer");
        }
    }
}

The output is:

Constant=1
Problem: static initializer
ReadOnly=2
Field=3
Property=4

(Tested against .NET 4.5.)


A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

Please note that a static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. and the user has no control on when the static constructor is executed in the program.

Taken from MSDN's Static Constructors (C# Programming Guide) .


My guess is that it's called before you expected it to be called. If you have already debugged your site but not recycled the AppPool, it's very likely that the static constructor has already been run. Similarly anything that accesses any static members will also call the static constructor if it hasn't already been called.