C#4: Why does this static field always get initialized over-eagerly?

Posted by TheSilverBullet on Programmers See other posts from Programmers or by TheSilverBullet
Published on 2012-11-30T09:07:53Z Indexed on 2012/11/30 11:20 UTC
Read the original article Hit count: 301

I am looking at this excellent article from Jon Skeet at this location: http://csharpindepth.com/Articles/General/Beforefieldinit.aspx

While executing the demo code, Jon Skeet says that we can expect three different kinds of behaviours. To quote that article:

The runtime could decide to run the type initializer on loading the assembly to start with... Or perhaps it will run it when the static method is first run... Or even wait until the field is first accessed...

When I try this out (on framework 4), I always get the first result. That is, the static method is initialized before the assembly is loaded. I have tried running this multiple times and get the same result. (I tried both the debug and release versions)

Why is this so? Am I missing something?

© Programmers or respective owner

Related posts about c#

Related posts about static