<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Emad Alashi&#039;s Blog &#187; static constructor</title>
	<atom:link href="http://www.emadashi.com/index.php/tag/static-constructor/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emadashi.com</link>
	<description></description>
	<lastBuildDate>Sun, 15 Jan 2012 10:05:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Beware of Static Constructors</title>
		<link>http://www.emadashi.com/index.php/2009/02/be-ware-of-static-constructors/</link>
		<comments>http://www.emadashi.com/index.php/2009/02/be-ware-of-static-constructors/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 22:39:22 +0000</pubDate>
		<dc:creator>Emad Alashi</dc:creator>
				<category><![CDATA[Bunian]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[code coverage]]></category>
		<category><![CDATA[static constructor]]></category>

		<guid isPermaLink="false">http://eashi.wordpress.com/2009/02/07/be-ware-of-static-constructors/</guid>
		<description><![CDATA[In Bunian we needed to use a static constructor for some reason, it was all going good; we tested the code and it ran smoothly&#8230;excellent (Code Coverage anyone?!).But when I came across this situation, it appeared that the static constructor wasn&#8217;t invoked!even when &#8220;Class.Method();&#8221; is called! so lets examine it.
I have two simple classes as [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.codeplex.com/bunian">Bunian</a> we needed to use a static constructor for some reason, it was all going good; we tested the code and it ran smoothly&#8230;excellent (<a href="http://en.wikipedia.org/wiki/Code_coverage">Code Coverage</a> anyone?!).<br />But when I came across this situation, it appeared that the static constructor wasn&#8217;t invoked!even when &#8220;Class.Method();&#8221; is called! so lets examine it.</p>
<p>I have two simple classes as an Example:</p>
<div style="font-size:10pt;background:#f2ebe3;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Parent</span></p>
<p style="margin:0;">{</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">string</span> DoSomething()</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> <span style="color:#a31515;">&#8220;Parent: DoSomething() called&#8221;</span>;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">}</p>
</div>
<p> 
<div style="font-size:10pt;background:#f2ebe3;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Child</span> : Parent</p>
<p style="margin:0;">{</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">static</span> Child()</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Child Static constructor called&#8221;</span>);</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">string</span> DoSomethingDifferent()</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span> <span style="color:#a31515;">&#8220;DoSomethingDifferent() called&#8221;</span>;</p>
<p style="margin:0;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin:0;">}</p>
</div>
<p>As it may be obvious, Child inherits from Parent, Child has a static constructor that we need to be executed when ever a method is invoked by Child. Now lets check the main program executing these two lines:</p>
<div style="font-size:10pt;background:#f2ebe3;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:#2b91af;">Console</span>.WriteLine(Child.DoSomething()); <span style="color:green;">//This code will NOT invoke the static constructor</span></p>
<p style="margin:0;"><span style="color:#2b91af;">Console</span>.WriteLine(Child.DoSomethingDifferent());<span style="color:green;">//This code WILL invoke the static constructor</span></p>
</div>
<p>The surprise (at least to me) when calling &#8220;Child.DoSomething()&#8221; the static constructor isn&#8217;t invoked! because it is in the parent!! aaaaaah! bad!! that was serious for our architecture&nbsp; and we had to do lots of fixes to turns things around the right way (which I think it was for our own good for other reasons <img src='http://www.emadashi.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> )</p>
<p>This brings up the <a href="http://en.wikipedia.org/wiki/Code_coverage">Code Coverage</a> topic as well; in our case that static constructor&#8217;s job was to create an instance of a member that is only needed once, and we check on it in other times by &#8220;if _instance != null&#8221;. It always ran ok because all the test code we created used to call an original Child before calling any other method that resided in the Parent.</p>
<p>bottom line: be ware of static constructors, and check your test code&#8230;it maybe hiding &#8220;surprises&#8221; for you <img src='http://www.emadashi.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadashi.com/index.php/2009/02/be-ware-of-static-constructors/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

