Not-So-Lazy Static Constructors in C# 4.0

Tags: .NET, C#, Lazy, Static Constructor

A coworker pointed me towards an interesting blog post by John Skeet about the changed behavior of static constructors in C# 4.0 (yes, I know, it’s been a few years now, but I never ran into it).

It seems that C# 4.0 now tries to be lazier when instantiated static fields. So if I have a side-effect free static method that doesn’t touch any members, calling it will not bother initializing the static fields. From Skeet’s post:

  1. class Lazy
  2.     {
  3.         private static int x = Log();
  4.         private static int Log()
  5.         {
  6.             Console.WriteLine("Type initialized");
  7.             return 0;
  8.         }
  9.         public static void StaticMethod()
  10.         {
  11.             Console.WriteLine("In static method");
  12.         }
  13.     }

Calling StaticMethod() will print “In static method”, but will not print “Type Initialized”!

This was a bit worrying. John Skeet said it shouldn’t impact existing code, but we were not so sure. Imagine a class whose static constructor does some unmanaged initialization work (creates a Performance Counter, for instance) while the static method  writes to the counter. This could potentially cause a hard to find exception, since our expectation was that static constructors can be relied upon to always be called first.

So we ran a quick test (and by “we” I most mean Amir), and it seems that this behavior isn’t as problematic as we thought. Look at this code, adding a static constructor to the class above:

  1. class Lazy
  2.     {
  3.         private static int x = Log();
  4.         private static int Log()
  5.         {
  6.             Console.WriteLine("Type initialized");
  7.             return 0;
  8.         }
  9.  
  10.         static Lazy()
  11.         {
  12.             Console.WriteLine("In static constructor");
  13.         }
  14.  
  15.         public static void StaticMethod()
  16.         {
  17.             Console.WriteLine("In static method");
  18.         }
  19.     }

The only difference is that we have an explicit static constructor, rather than the implicit one that initializes the field. Unlike the first test case, in this case calling StaticMethod() did call the static constructor, and we could see “In static constructor” printed before “In static method”. The compiler is smart enough to see that we have an explicit constructor defined, so that means we want it to be called, and it will be called. This was reassuring.

But wait, there’s more! It seems that once type initialization was triggered by the presence of the explicit static constructor, it went all the way. Even the x parameter was initialized, the Log() method was called, and “Type Initialized” was printed to the console, even before the static constructor. This was the behavior I was used to, where static field initializations are added to the beginning of the .cctor.

To summarize, the new lazy type initialization behavior for C# 4.0 is interesting, since it allows static classes that contain only side-effect free methods (for instance, classes containing popular Extension Methods) to avoid expensive and unnecessary initializations. But it was designed smartly enough to recognize when initialization is explicitly desired, and be a bit less lazy in that case.

(And thanks again to Igal Tabachnik and Amir Zuker)

No Comments