<?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>Geekality &#187; Null</title>
	<atom:link href="http://www.geekality.net/tag/null/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekality.net</link>
	<description>With a hint of Social Ineptitude</description>
	<lastBuildDate>Tue, 27 Jul 2010 16:15:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Generics and checking for null</title>
		<link>http://www.geekality.net/2009/11/13/generics-and-checking-for-null/</link>
		<comments>http://www.geekality.net/2009/11/13/generics-and-checking-for-null/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 16:19:15 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Null]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=788</guid>
		<description><![CDATA[When writing C#, in Visual Studio, using generics&#8230; have you ever tried checking for null? I have always found that a bit of a hassle. Say we have this method which returns the subject if it is not null, and &#8230; <a href="http://www.geekality.net/2009/11/13/generics-and-checking-for-null/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When writing C#, in Visual Studio, using generics&#8230; have you ever tried checking for null? I have always found that a bit of a hassle. </p>
<p>Say we have this method which returns the subject if it is not null, and the result of a <code class="codecolorer text default"><span class="text">createNew()</span></code> function if it <em>is</em> null.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>subject <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> createNew<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Not claiming this is a very useful method, but it was the simplest thing I could come up with <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> Anyways, in Visual Studio you will now probably have a blue (is blue here at least) squiggly line under the <a href="http://msdn.microsoft.com/en-us/library/53k8ybth.aspx">equality operator</a>. It states you are doing a <cite>Possible compare of value type with &#8216;null&#8217;</cite>, which of course is reasonable and correct. We <em>could</em> just ignore it and move on&#8230; but we don&#8217;t really like squiggly lines, do we? I sure don&#8217;t&#8230; so lets get rid of it.</p>
<p><span id="more-788"></span></p>
<h2>Faulty solution</h2>
<p>ReSharper suggests that I use <a href="http://msdn.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx">default(T)</a> instead of null, like this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>subject <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">default</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> createNew<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Now, this won&#8217;t do at all. First of all, we don&#8217;t really want to check for <a href="http://msdn.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx">default(T)</a>, we want to check for null. And second of all, we now have a long red squiggly line under the whole equality statement! It says that it <cite>Cannot apply operator &#8216;==&#8217; to operands of type &#8216;T&#8217; and &#8216;T&#8217;</cite>. That messages doesn&#8217;t really make much sense to me, to be honest, but I trust that it is true. Either way, we have to get rid of it.</p>
<h2>Good solution</h2>
<p><a href="http://msdn.microsoft.com/en-us/library/system.object.referenceequals(VS.80).aspx">ReferenceEquals</a> to the rescue! We can use that method, instead of the equality operator, and compare with null like we intended! <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> </p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ReferenceEquals<span style="color: #008000;">&#40;</span>subject, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> createNew<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>That is kind of ugly though&#8230; And it is <em>so</em> much longer to type. <em>And</em>, most importantly, it kind of makes the code a bit unreadable&#8230; At least if you have a bunch of things you have to check for null, like I did today.</p>
<p>But fear not! Today I came up with a tiny, but brilliant extension method which makes things a lot better:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> T subject<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> ReferenceEquals<span style="color: #008000;">&#40;</span>subject, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>subject<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNull</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> createNew<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Doesn&#8217;t that just look <em>a lot</em> cleaner? I think so <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> </p>
<p>And yes, I know this isn&#8217;t really a big issue. And I realize you have probably all come up with something similar already. Yes, I am slow <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> But hey, I figured it might be worth sharing this anyways, just in case you hadn&#8217;t thought about it before. I sure hadn&#8217;t! And I really do think that tiny snippet of code made a huge difference in code readability. And we all want that, don&#8217;t we? Yes, yes we do&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/13/generics-and-checking-for-null/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
