Interesting fact that I discovered today: usually, static field initializers run whenever the parent type is first loaded. However when you ngen the assembly, apparently you can load the type and work with it – static field initializers will only be executed immediately before you actually access the field.
I’ve run into this by having a static Stopwatch timer = Stopwatch.StartNew(); in one of my types. It was supposed to measure an application’s startup time, but I noticed that after NGEN’ing the assembly the timer always returned a value close to 0.
Here’s the code to verify this behavior:
private static int field = Initialize("Field initializer");
static void Main(string args)
field = 1; // touch the field
private static int Initialize(string message)
Console.WriteLine(message + ": " + DateTime.Now.Ticks.ToString());
This works the same for .NET 4.0 SP1 for Debug/Release and x86/x64. I didn’t try other frameworks/OS/platforms.
Here’s the output from the console:
Field initializer: 634462768999291588
Field initializer: 634462769013544438
E:\>ngen install NgenedStaticFieldInitializers.exe
Microsoft (R) CLR Native Image Generator - Version 4.0.30319.1
Copyright (c) Microsoft Corporation. All rights reserved.
Installing assembly E:\NgenedStaticFieldInitializers.exe
1> Compiling assembly E:\NgenedStaticFieldInitializers.exe (CLR v4.0.30319) .
1>NgenedStaticFieldInitializers, Version=188.8.131.52, Culture=neutral, PublicKeyToke
Field initializer: 634462769095660858
Field initializer: 634462769108933512
I believe *all* static field initializers will run at the same time - but when that occurs is only guaranteed to be "before the first static field access or constructor call" unless you've got a static constructor, which changes the timing a bit.
All of this changed (in implementation) in .NET 4 - I wasn't aware that ngen makes the situation even more complicated though. I wonder whether that is mostly about the entry point type rather than other types...
Here's my write-up from a while ago:
From what I understand, and I can't find the blog post that i always refer to when dealing with static initializers, is that the timing of static field initializers is undefined unless you have a static constructor. The only thing that is guarenteed without a static constructor is that the field will be initialized before it's first accessed. With a static constructor the static fields are initialized before the static contructor is executed. This all has something to do with BeforeFieldInit, and this post is the closest I found to a decent explanation : www.satyakomatineni.com/.../display
Thanks for the great info, guys!