Static constructors, part two

The first odd case I want to talk about involves static methods. Take a look at the sample program from last time. Now suppose we edited the Main method to say:

static void Main()
{
D.M();
}

First off, is that even legal? Sure! Inheritance means that all inheritable members of B are also members of D. M is an inheritable member of B, so it is a member of D, right?

Unfortunately, this corner case is the one that exposes the leaky abstraction. The compiler generates code as though you had said B.M();, and therefore D’s static constructor is not called even though “a member of D” has been invoked. This actually makes a fair amount of sense. The method B.M is going to be called, and there’s no reason to go to all the work of running D’s static constructor when B.M probably does not depend on any work done by D’s constructor. And it would seem strange if calling the same method by two different syntaxes would result in different static constructor invocations.

Now let’s consider a second case involving static method invocation. Suppose now we edited Main to say:

static void Main()
{
D.N();
}

Clearly D’s static constructor must be invoked. What about B? Is its static constructor invoked? No! A static constructor is triggered by a usage of a static member, or by the creation of an instance. Invoking D.N does not use any static member of B and it does not create an instance of B, so B’s static constructor is not invoked. People sometimes expect that static constructors of base classes will always be invoked before static constructors of derived classes, but that’s not the case.

Our third odd case is: what happens when a static constructor throws an exception?

Absolutely nothing good! First off, of course if the exception goes unhandled then all bets are off. The runtime is permitted to do anything it likes if there is an unhandled exception, including such options as starting up a debugger, terminating the appdomain immediately, terminating the application after running finally blocks, and so on. And an exception in a static constructor can easily go unhandled; trying to wrap every possible first usage of a type with a try-catch block is onerous.

And even if by some miracle the exception gets handled the first time, odds are very good that your program is now in such a damaged state that it is going to go down in flames soon. Remember, I said that a static constructor runs once, and by that I meant once; if it throws, you don’t get a second chance. Instead, when a static constructor terminates abnormally, the runtime marks the type as unusable, and every attempt by your program to use that type results in another exception.

“And it would seem strange if calling the same method by two different syntaxes would result in different static constructor invocations.”

It also seems strange that calling two static methods through the same type could result in different static constructors executing. A user could reasonably expect that both

static void Main() { D.M(); }

and:

static void Main() { D.N(); }

would each invoke the same static constructors (whether B’s is included or not is a separate matter). In this case, part of me thinks that by calling M() through D, the user has made a choice, perhaps unconsciously, that D needs to be initialized before the call to M(). Another part of me thinks that C# was designed very carefully to avoid such subtleties. Was there more to this decision than just being able to avoid the runtime cost of invoking D’s static initializers unnecessarily?

-1- Static constructors for value types do not seem to run run until an instance property or method [but not field] is accessed, a parameterized constructor is used, `Activator.CreateInstance()` is called, or a static member [including field] of the type is accessed, even though value-type instances can come into existence and (if fields are exposed) be used without any of those things happening.

-2- The instantiation of T[] does not imply the creation of type T, whether T is a class or a struct.

Somewhat related to point 2, if one has a generic method with a type parameter `T` that may or may not have a public constructor, is there a nice way for that method to force type `T` to be constructed if it hasn’t been yet?