#878 – Unhandled Exceptions in Static Constructors

If any exception is thrown from within a static constructor, a TypeInitializationException will be thrown, with an InnerException set to the original exception that occurred.  If this exception is not caught, the application will be terminated.

    public class Dog
    {
        public static string Motto { get; set; }

        static Dog()
        {
            Motto = "Serve humans and chase balls";
            throw new Exception("Static Dog constructor threw me");
        }
    }

878-001

#817 – What Happens When a Static Constructor Throws an Exception

If any exception is thrown from within a static constructor, a TypeInitializationException will be thrown, with an InnerException set to the original exception that occurred.  At that point, you can no longer access any static data or methods in the class.  You also can’t create any objects of the type using instance constructors.  After the exception, the type is unusable.

    public class Dog
    {
        public static string DogMotto { get; set; }

        static Dog()
        {
            DogMotto = "We serve humans";
            throw new Exception("Oh no");
        }
    }
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine(Dog.DogMotto);
                Console.WriteLine("After DogMotto output");
            }
            catch (Exception xx)
            {
                Console.WriteLine("Error: {0}", xx.ToString());
            }

817-001

#670 – Static vs. Instance Initialization

Static fields and properties are data members associated with a class itself, rather with instances of that class.  A static constructor can be used to initialize static members.  Static fields can also be initialized when they are declared.

The first time that you reference a static member, the following occurs:

  • All static fields are initialized
  • The static constructor is called
  • Your code, which accesses the static member, executes

If you reference an instance method, or create an instance of the class, before referencing any static members, the following occurs:

  • All static fields are initialized
  • The static constructor is called
  • All instance fields are initialized
  • The instance constructor is called
  • Your code, which accesses an instance member, executes

For example, initializing an instance of a Dog leads to the output shown below.

Dog d = new Dog();

#292 – A Static Constructor Initializes Static Data

In the same way that you can define instance constructors to initialize instance data in a class, you can define a static constructor to initialize any static data in the class.

A static constructor is defined using the static keyword and the name of the class.  The constructor takes no arguments and you can only define one static constructor.  You do not declare a static constructor as either public or private.

        public static string Motto { get; set; }
        public static int NumDogs { get; private set; }

        static Dog()
        {
            Motto = "We're like wolves.  Only friendlier.";
            NumDogs = 0;
        }

You cannot dictate when a static constructor will be called and you can’t call it directly.  The compiler guarantees that it will be called automatically before you create any instances of your class or access any static data.