<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx</link><description>&amp;lt;Added additional stuff after a discussion on internal CSharp user list&amp;gt; 
 I was going through the generics spec in C#2.0 and found something really weird. 
 default(t) 
 Consider the following code. class MyGenClass &amp;lt;T&amp;gt; { public void Method</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx#10225941</link><pubDate>Sun, 16 Oct 2011 16:34:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10225941</guid><dc:creator>Ari</dc:creator><description>&lt;p&gt;for those who might stumble upon this through search, this might help:&lt;/p&gt;
&lt;p&gt;here is a solution: &lt;a rel="nofollow" target="_new" href="http://www.geekality.net/2009/11/13/generics-and-checking-for-null/"&gt;www.geekality.net/.../generics-and-checking-for-null&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225941" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx#10111060</link><pubDate>Mon, 03 Jan 2011 09:46:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10111060</guid><dc:creator>hagen</dc:creator><description>&lt;p&gt;to avoid comparing to &amp;quot;null&amp;quot;, you might use EqualityComparer&amp;lt;T&amp;gt;.Default.Equals(T x, default(T))&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10111060" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx#9940277</link><pubDate>Tue, 22 Dec 2009 22:29:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9940277</guid><dc:creator>Dan</dc:creator><description>&lt;p&gt;If a Generic Type T is implementing an interface, it would therefore assume it was a reference type, and thus nullable? isn't this correct.&lt;/p&gt;
&lt;p&gt;i.e.&lt;/p&gt;
&lt;p&gt;public T x&amp;lt;T&amp;gt;() where T : IMyInterface&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return null;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9940277" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx#9396277</link><pubDate>Wed, 04 Feb 2009 21:12:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9396277</guid><dc:creator>GuyBehindtheGuy</dc:creator><description>&lt;p&gt;Thanks for the info! &amp;nbsp;I just ran into this exact problem.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9396277" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx#9063287</link><pubDate>Wed, 12 Nov 2008 19:39:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9063287</guid><dc:creator>Jonathan Delorme</dc:creator><description>&lt;p&gt;// The clean way ;)&lt;/p&gt;
&lt;p&gt;public static bool IsDefaultValue(T res)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;return Object.Equals(res, null);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9063287" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx#8524713</link><pubDate>Wed, 21 May 2008 00:25:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8524713</guid><dc:creator>Kc Joe</dc:creator><description>&lt;p&gt;Oops ... Fixed:&lt;/p&gt;
&lt;p&gt;public static bool IsDefaultValue(T res)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//Compiler Error&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//return (default(T) == res);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var obj = default(T) as object;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (obj == null) //Is Not Value Type&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (res == null);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (obj.GetType().IsValueType)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (res == null) //res Is Not A Value Type&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return res.Equals(obj);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Debug.Fail(&amp;quot;Should Not Reach Here!&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;Still Ugly!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8524713" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx#8524689</link><pubDate>Wed, 21 May 2008 00:19:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8524689</guid><dc:creator>Kc Joe</dc:creator><description>&lt;p&gt;So If You'd Like To Check Inside A Generic Class If A Variable Eqauls To Default You Should Do:&lt;/p&gt;
&lt;p&gt;public bool IsDefault(TInterpretationResult res)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//Compiler Error&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//return (default(T) == res);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var obj = default(T) as object;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (obj == null) //Is Not Value Type&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (res == null);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (obj.GetType().IsValueType)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (ValueType)obj == (ValueType)obj;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Debug.Fail(&amp;quot;Should Not Reach Here!&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;Seems Kinda Ugly, It's Strange That MS Didn't Handle It In A More Elegent Way&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8524689" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx#6675249</link><pubDate>Thu, 06 Dec 2007 08:54:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6675249</guid><dc:creator>jokiz</dc:creator><description>&lt;p&gt;&amp;quot;The int gets converted to a nullable type&amp;quot; - nope, i checked the IL and it's not, still int32&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6675249" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx#3783072</link><pubDate>Mon, 09 Jul 2007 19:08:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3783072</guid><dc:creator>Sunny</dc:creator><description>&lt;p&gt;I want to do something like:&lt;/p&gt;
&lt;p&gt;class myObject{&lt;/p&gt;
&lt;p&gt; public T Fetch&amp;lt;T&amp;gt;(ICriteria oCriteria){&lt;/p&gt;
&lt;p&gt; &amp;nbsp; T fetchResult = default(T);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; if(T == something){&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; fetchResult = doSomething();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; }&lt;/p&gt;
&lt;p&gt; private something doSomething(){&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return new something();&lt;/p&gt;
&lt;p&gt; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;which gives me a compile time error at fetchResult = doSomething(); &lt;/p&gt;
&lt;p&gt;Any help?&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Sunny&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3783072" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Generics, default(T) and compare to null weirdness</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/12/11/501544.aspx#1988879</link><pubDate>Thu, 29 Mar 2007 17:46:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1988879</guid><dc:creator>Jochen</dc:creator><description>&lt;p&gt;default(T).Equals(obj);&lt;/p&gt;
&lt;p&gt;DOES NOT WORK if T is a reference type since you cannot call .Equals on null...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1988879" width="1" height="1"&gt;</description></item></channel></rss>