{"id":2519,"date":"2007-08-03T05:12:00","date_gmt":"2007-08-03T05:12:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/say-hello-static-constructor\/"},"modified":"2016-07-28T10:49:08","modified_gmt":"2016-07-28T10:49:08","slug":"say-hello-static-constructor","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/say-hello-static-constructor\/","title":{"rendered":"Say hello, static constructor?"},"content":{"rendered":"<p>When does the static constructor of your .NET class actually get called?<\/p>\n<p>Sounds like it should be pretty obvious really &#8211; at least going on the side of &#8220;how late can it possibly be called&#8221;. Most of us are used to the idea that static constructors are called before any other methods on that class are called. I know I hadn&#8217;t given it too much more thought than that.<\/p>\n<p>According to the <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/k9x6w0hc%28vs.80%29.aspx\">MSDN page on static constructors<\/a>, they will be &#8220;&#8230;called automatically to initialize the class before the first instance is created or any static members are referenced&#8221;.<\/p>\n<p>However, what I didn&#8217;t realise is the implications this had when you start calling static methods on subclasses of your types. Consider the following code:<\/p>\n<pre>static class Program{\tstatic void Main()\t{\t\tSub.SayHello();\t\tSub.NoOp();\t\tSub.SayHello();\t\tConsole.ReadLine();\t}}class Super{\tprotected static string m_String = \"\";\tstatic Super()\t{\t\tConsole.WriteLine(\"Super..cctor() called\");\t}\tpublic static void SayHello()\t{\t\tConsole.WriteLine(m_String);\t}}class Sub : Super{\tstatic Sub()\t{\t\tConsole.WriteLine(\"Sub..cctor() called\");\t\tm_String = \"Hello world!\";\t}\tpublic static void NoOp()\t{\t}}<\/pre>\n<p>Now, if you run that, I would have expected to see something along the lines of:<br \/>Super..cctor() called<br \/>Sub..cctor() called<br \/>Hello world!<br \/>Hello world!<br \/>But no! What you actually get is:<br \/>Super..cctor() called<br \/>&lt;Not set yet&gt;<br \/>Sub..cctor() called<br \/>Hello world!<br \/>In other words, <i>calling a static method on a subclass that is defined on the superclass does not cause the subclass&#8217;s static constructor to execute<\/i>! Only when the first member of the subclass is accessed is the static constructor called.<\/p>\n<p>Now, the interesting thing is that if you look at it in Reflector, you&#8217;ll see that the compiler has actually called Super.SayHello() rather than Sub.SayHello() as in the original source:<\/p>\n<pre>private static void Main(){    Super.SayHello();    Sub.NoOp();    Super.SayHello();    Console.ReadLine();}<\/pre>\n<p>I guess this is a performance win, since the inheritance hierarchy can be traversed at compile-time rather than design-time, but I&#8217;m a bit sceptical. Does this really honour the contract that the static constructor will be called before &#8220;any static members are referenced&#8221;? Let me know what you think!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When does the static constructor of your .NET class actually get called? Sounds like it should be pretty obvious really &#8211; at least going on the side of &#8220;how late can it possibly be called&#8221;. Most of us are used to the idea that static constructors are called before any other methods on that class&#8230;&hellip;<\/p>\n","protected":false},"author":169277,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2519","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/169277"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2519"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2519\/revisions"}],"predecessor-version":[{"id":24488,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2519\/revisions\/24488"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2519"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}