Answer: Initializing static fields correctly

Well – quite a good discussion.  This is one of the reasons I love blogs.  I really learned something today based on your feedback and we made a change to the product because of it.  So thanks for your feedback. 


As many of you said, C1 and C2 result in the exact same behavior.  The only difference is perf.  Because C1 causes a type constructor (.cctor) to be created and the load time negative effected and possibilities for optimization are limited.  So I was thinking that the answer should be C2 is better.  But a couple of you pointed out that C1 is more explicit and matches “best practice” for C++ developers for years.  So really what you want is the perf of C2, but the expressiveness of C1. 


So I forwarded your feedback to the C# language designers and they quickly saw based on your comments it was a good idea. Peter Hallam the C# dev lead posted about the fix they chose.  Oh, and for you VB and C++ heads, I did of course pass the feedback on to them as well. 


So thanks and keep the comments coming, you are directly making the product better, even if in small ways.

Comments (4)

  1. Nice. Blogs at work 🙂

  2. Judah says:

    Awesome. This kind of feedback + immediate response is great, I’ve never seen this sort of responsivness to developers from any company or OSS community before.

  3. i still want "private static Foo _instance = new Foo()" to be the One True Way to implement singleton with no need for the "add an empty static constructor to avoid beforefieldinit" – IMHO the "fourth version" @ is almost perfect – if only we didn’t require that pesky empty static ctor 🙂

  4. Cory Smith says:

    As one of the VB heads following this… thanks for thinking of us 😀

Skip to main content