<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: .NET Misconceptions Part 1</title>
	<atom:link href="http://nickgravelyn.com/2009/04/net-misconceptions-part-1/feed/" rel="self" type="application/rss+xml" />
	<link>http://nickgravelyn.com/2009/04/net-misconceptions-part-1/</link>
	<description></description>
	<lastBuildDate>Wed, 10 Mar 2010 20:04:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Nick</title>
		<link>http://nickgravelyn.com/2009/04/net-misconceptions-part-1/comment-page-1/#comment-275</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Thu, 28 May 2009 00:47:13 +0000</pubDate>
		<guid isPermaLink="false">http://nickgravelyn.com/?p=662#comment-275</guid>
		<description>If I have EnumA.Value that happens to be equal to integer 1 and EnumB.Value that happens to be equal to integer 1, the framework automatically casting those to int to compare is going to either require additional instructions to ensure the two integers stemmed from the same enumeration (thus being less performant) or it will have to ignore type safety and consider EnumA.Value to be equal to EnumB.Value simply because their underlying integer values are the same. The third option is to box and compare using Object.Equals which is exactly what the framework does automatically, but this incurs the boxing/unboxing cost.

People are free to ignore it, but considering you could easily copy/paste the code and change a few types it&#039;s rather quick considering the number of times those people are going to keep casting their enums to ints (which is also throwing type safety out the window). If you type (int) about a dozen or so times, you&#039;ve likely typed just about as much as that class; more if you just copy/paste it and change the type to the new enum. In that sense it&#039;s almost more appropriate that lazy developers would implement this whenever using an enumeration for a dictionary key.</description>
		<content:encoded><![CDATA[<p>If I have EnumA.Value that happens to be equal to integer 1 and EnumB.Value that happens to be equal to integer 1, the framework automatically casting those to int to compare is going to either require additional instructions to ensure the two integers stemmed from the same enumeration (thus being less performant) or it will have to ignore type safety and consider EnumA.Value to be equal to EnumB.Value simply because their underlying integer values are the same. The third option is to box and compare using Object.Equals which is exactly what the framework does automatically, but this incurs the boxing/unboxing cost.</p>
<p>People are free to ignore it, but considering you could easily copy/paste the code and change a few types it&#8217;s rather quick considering the number of times those people are going to keep casting their enums to ints (which is also throwing type safety out the window). If you type (int) about a dozen or so times, you&#8217;ve likely typed just about as much as that class; more if you just copy/paste it and change the type to the new enum. In that sense it&#8217;s almost more appropriate that lazy developers would implement this whenever using an enumeration for a dictionary key.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SocialLonerStudios</title>
		<link>http://nickgravelyn.com/2009/04/net-misconceptions-part-1/comment-page-1/#comment-274</link>
		<dc:creator>SocialLonerStudios</dc:creator>
		<pubDate>Thu, 28 May 2009 00:34:46 +0000</pubDate>
		<guid isPermaLink="false">http://nickgravelyn.com/?p=662#comment-274</guid>
		<description>The main problem I see with this solution is that most developers are lazy (which includes me sometimes).  Implementing an entire new class just to get an equal statement to work more efficiently seems crazy (that&#039;s a lot of typing).  Especially when the underlying data type of an Enum is an Integer.  Why doesn&#039;t the framework do this for us automatically?  Casting it to an int is a lot easier and developers are going to keep doing it.  I do agree with you btw, people just aren&#039;t going to do it.</description>
		<content:encoded><![CDATA[<p>The main problem I see with this solution is that most developers are lazy (which includes me sometimes).  Implementing an entire new class just to get an equal statement to work more efficiently seems crazy (that&#8217;s a lot of typing).  Especially when the underlying data type of an Enum is an Integer.  Why doesn&#8217;t the framework do this for us automatically?  Casting it to an int is a lot easier and developers are going to keep doing it.  I do agree with you btw, people just aren&#8217;t going to do it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Omer Mor</title>
		<link>http://nickgravelyn.com/2009/04/net-misconceptions-part-1/comment-page-1/#comment-273</link>
		<dc:creator>Omer Mor</dc:creator>
		<pubDate>Tue, 19 May 2009 20:27:35 +0000</pubDate>
		<guid isPermaLink="false">http://nickgravelyn.com/?p=662#comment-273</guid>
		<description>Oh, I didn&#039;t know the xbox 360 was using the compact edition of the framework.</description>
		<content:encoded><![CDATA[<p>Oh, I didn&#8217;t know the xbox 360 was using the compact edition of the framework.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://nickgravelyn.com/2009/04/net-misconceptions-part-1/comment-page-1/#comment-272</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Mon, 18 May 2009 20:52:39 +0000</pubDate>
		<guid isPermaLink="false">http://nickgravelyn.com/?p=662#comment-272</guid>
		<description>The problem with that solution is that it relies on Reflection.Emit which doesn&#039;t exist on the Xbox 360. It&#039;s also quite complex and while it might save you time if you utilize a lot of enums as keys, it&#039;s probably faster to just roll a comparer when you need one since most people likely only need a few of them. If you create lots of them, you could probably also make a VS file template that generates the whole thing for you, leaving you to just fill in the type.</description>
		<content:encoded><![CDATA[<p>The problem with that solution is that it relies on Reflection.Emit which doesn&#8217;t exist on the Xbox 360. It&#8217;s also quite complex and while it might save you time if you utilize a lot of enums as keys, it&#8217;s probably faster to just roll a comparer when you need one since most people likely only need a few of them. If you create lots of them, you could probably also make a VS file template that generates the whole thing for you, leaving you to just fill in the type.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Omer Mor</title>
		<link>http://nickgravelyn.com/2009/04/net-misconceptions-part-1/comment-page-1/#comment-271</link>
		<dc:creator>Omer Mor</dc:creator>
		<pubDate>Mon, 18 May 2009 20:12:34 +0000</pubDate>
		<guid isPermaLink="false">http://nickgravelyn.com/?p=662#comment-271</guid>
		<description>Instead of rolling your own hand-written EnumComparers, you can use a generic implementation that fits all enums. It&#039;s not trivial to write, but I provided 2 examples that use lightweight code-generation techniques in a CodeProject article that I recently wrote.
You should check it out here:
http://www.codeproject.com/KB/cs/EnumComparer.aspx</description>
		<content:encoded><![CDATA[<p>Instead of rolling your own hand-written EnumComparers, you can use a generic implementation that fits all enums. It&#8217;s not trivial to write, but I provided 2 examples that use lightweight code-generation techniques in a CodeProject article that I recently wrote.<br />
You should check it out here:<br />
<a href="http://www.codeproject.com/KB/cs/EnumComparer.aspx" rel="nofollow">http://www.codeproject.com/KB/cs/EnumComparer.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: darrenecm</title>
		<link>http://nickgravelyn.com/2009/04/net-misconceptions-part-1/comment-page-1/#comment-254</link>
		<dc:creator>darrenecm</dc:creator>
		<pubDate>Mon, 20 Apr 2009 11:11:17 +0000</pubDate>
		<guid isPermaLink="false">http://nickgravelyn.com/?p=662#comment-254</guid>
		<description>Aha, I understand now.

I guess the moral of the story for inexperienced C# and .NET coders learning XNA is to keep an eye out for all .NET data structures and classes whose implentation might be using that pesky System.Object as a default type and, where possible, provide your own specific type based on the needs of your program.

I&#039;m slowly realising the generalised nature of System.Object can be great for making life somewhat easier for the more sedentary event-driven applications, or maybe even in areas of XNA code that are called once, but you sure as heck do not want one living in tight code loops such as Update and Draw that are called frequently :)

Thanks for taking the time to give some insight into this.</description>
		<content:encoded><![CDATA[<p>Aha, I understand now.</p>
<p>I guess the moral of the story for inexperienced C# and .NET coders learning XNA is to keep an eye out for all .NET data structures and classes whose implentation might be using that pesky System.Object as a default type and, where possible, provide your own specific type based on the needs of your program.</p>
<p>I&#8217;m slowly realising the generalised nature of System.Object can be great for making life somewhat easier for the more sedentary event-driven applications, or maybe even in areas of XNA code that are called once, but you sure as heck do not want one living in tight code loops such as Update and Draw that are called frequently <img src='http://nickgravelyn.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks for taking the time to give some insight into this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://nickgravelyn.com/2009/04/net-misconceptions-part-1/comment-page-1/#comment-253</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Sun, 19 Apr 2009 14:47:50 +0000</pubDate>
		<guid isPermaLink="false">http://nickgravelyn.com/?p=662#comment-253</guid>
		<description>Good point. The garbage that occurs is because of boxing/unboxing costs under the hood. The dictionary requires an IEqualityComparer&lt;T&gt; in order to work properly because it obviously needs to be able to compare keys. Since there isn&#039;t a way to constrain your TKey type to having implemented the == sign, you need some other way of testing equality. 

IEqualityComparer&lt;T&gt; lets you determine if two objects are equal. That&#039;s really all the interface is for. A dictionary will provide a default IEqualityComparer&lt;T&gt; using the EqualityComparer&lt;T&gt;.Default property if you do not provide one on your own. The comparer that is generated first determines if T implements IEquatable and if so uses that to determine equality. If T does not implement IEquatable (as is the case for all enumerations) the EqualityComparer&lt;T&gt;.Default will have an implementation that relies on the object.Equals override available for the type, which for enumerations means they take in type object. Passing the enumeration as an object will cause boxing and thus create garbage.

Thus if you pass in your own implementation of IEqualityComparer&lt;T&gt; the dictionary will use that instead, so if we just give it a comparer that doesn&#039;t rely on the object.Equals method, we avoid it using the default implementation and the garbage it creates.</description>
		<content:encoded><![CDATA[<p>Good point. The garbage that occurs is because of boxing/unboxing costs under the hood. The dictionary requires an IEqualityComparer&lt;T&gt; in order to work properly because it obviously needs to be able to compare keys. Since there isn&#8217;t a way to constrain your TKey type to having implemented the == sign, you need some other way of testing equality. </p>
<p>IEqualityComparer&lt;T&gt; lets you determine if two objects are equal. That&#8217;s really all the interface is for. A dictionary will provide a default IEqualityComparer&lt;T&gt; using the EqualityComparer&lt;T&gt;.Default property if you do not provide one on your own. The comparer that is generated first determines if T implements IEquatable and if so uses that to determine equality. If T does not implement IEquatable (as is the case for all enumerations) the EqualityComparer&lt;T&gt;.Default will have an implementation that relies on the object.Equals override available for the type, which for enumerations means they take in type object. Passing the enumeration as an object will cause boxing and thus create garbage.</p>
<p>Thus if you pass in your own implementation of IEqualityComparer&lt;T&gt; the dictionary will use that instead, so if we just give it a comparer that doesn&#8217;t rely on the object.Equals method, we avoid it using the default implementation and the garbage it creates.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: darrenecm</title>
		<link>http://nickgravelyn.com/2009/04/net-misconceptions-part-1/comment-page-1/#comment-252</link>
		<dc:creator>darrenecm</dc:creator>
		<pubDate>Sun, 19 Apr 2009 14:20:44 +0000</pubDate>
		<guid isPermaLink="false">http://nickgravelyn.com/?p=662#comment-252</guid>
		<description>It&#039;s always cool to find solutions to common misconceptions about XNA, NET and C#, they help newcomers quickly avoid picking up bad habits and practices. Far too many online tutorials and books on XNA simply speak about how to use the XNA API in general terms, avoiding the less obvious topics and pitfalls to be aware of, such as garbage generation, why it&#039;s bad practice to use XNA components for all your objects etc.

However, I feel that many newcomers to XNA might also be fairly new to C# and .NET, so would it be possible when covering topics like this one to not only describe the immediate &#039;solution&#039;, but also have a preliminary chat about the &#039;Why&#039; you need to do it and some talk of &#039;How&#039; it works?

I&#039;m not familiar with the IEqualityComparer interface and the MSDN help files are less than enlightening to this newcomer, so it&#039;s not entirely clear why your solution works without some background into the problem it&#039;s solving. For example I thought in using Generics you avoid any boxing / unboxing gargage generation purely by virtue of using a Generic because it&#039;s using a specific type rather than System.Object?

Keep up the great work, I for one hugely appreciate it.</description>
		<content:encoded><![CDATA[<p>It&#8217;s always cool to find solutions to common misconceptions about XNA, NET and C#, they help newcomers quickly avoid picking up bad habits and practices. Far too many online tutorials and books on XNA simply speak about how to use the XNA API in general terms, avoiding the less obvious topics and pitfalls to be aware of, such as garbage generation, why it&#8217;s bad practice to use XNA components for all your objects etc.</p>
<p>However, I feel that many newcomers to XNA might also be fairly new to C# and .NET, so would it be possible when covering topics like this one to not only describe the immediate &#8217;solution&#8217;, but also have a preliminary chat about the &#8216;Why&#8217; you need to do it and some talk of &#8216;How&#8217; it works?</p>
<p>I&#8217;m not familiar with the IEqualityComparer interface and the MSDN help files are less than enlightening to this newcomer, so it&#8217;s not entirely clear why your solution works without some background into the problem it&#8217;s solving. For example I thought in using Generics you avoid any boxing / unboxing gargage generation purely by virtue of using a Generic because it&#8217;s using a specific type rather than System.Object?</p>
<p>Keep up the great work, I for one hugely appreciate it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
