<?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>FerretArmy: A Web Developer&#039;s Paradise &#187; HybridDictionary</title>
	<atom:link href="http://www.ferretarmy.com/tag/hybriddictionary/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ferretarmy.com</link>
	<description>Pushing the Web Forward, Since 2007</description>
	<lastBuildDate>Wed, 18 Jan 2012 15:58:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>HybridDictionaries &#8211; Careful!</title>
		<link>http://www.ferretarmy.com/2008/07/31/hybriddictionaries-careful/</link>
		<comments>http://www.ferretarmy.com/2008/07/31/hybriddictionaries-careful/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 02:59:07 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Geeky]]></category>
		<category><![CDATA[HybridDictionary]]></category>
		<category><![CDATA[generics]]></category>

		<guid isPermaLink="false">http://www.ferretarmy.com/2008/07/31/hybriddictionaries-careful/</guid>
		<description><![CDATA[I was refactoring some code the other day, and I noticed that at one point, there was code that created a foreach loop, but it was keeping track of an index inside, by manually incrementing at the end of the foreach loop. Something like this: int index = 0; foreach (Widget widget in WidgetBasket) { [...]]]></description>
			<content:encoded><![CDATA[<p>I was refactoring some code the other day, and I noticed that at one point, there was code that created a foreach loop, but it was keeping track of an index inside, by manually incrementing at the end of the foreach loop. Something like this:</p>
<blockquote><p>int index = 0;</p>
<p>foreach (Widget widget in WidgetBasket)</p>
<p>{</p>
<blockquote><p>// Do stuff&#8230;</p>
<p>index += 1;</p></blockquote>
<p>}</p></blockquote>
<p>Well, being the foolhardy coder that I was, I said let&#8217;s just change that foreach loop to a for loop, so we can keep track of the index as a part of the loop construct. I&#8217;ve done this a thousand times, so I didn&#8217;t think much of it. I also didn&#8217;t take a minute to look at the data type of Widget, not that it would have helped at that point.</p>
<p>So, I threw my code into test, and lo and behold I started getting null reference exceptions. What the heck &#8211; all I did was change a little loop?!?</p>
<p>Well, I dove under the covers, and it only took me a few minutes to find the issue. The Widget object was of type <a href="http://msdn.microsoft.com/en-us/library/system.collections.specialized.hybriddictionary.aspx" title="msdn - HybridDictionary">HybridDictionary</a>. If you&#8217;re not familiar with a HybridDictionary, it&#8217;s one of those built-for-speed collection types, behaving like aListDictionary when it&#8217;s small, and switching over to a HashTable when it gets larger. Like all collections I&#8217;m aware of, it has an array-style indexer, which enables it&#8217;s use in looping constructs.</p>
<p>The rub with HybridDictionaries is that they don&#8217;t behave like 0-based indexed array &#8211; a foreach will keep returning the next found item until it&#8217;s done, but a for loop doesn&#8217;t behave like that &#8211; the 0th element was null, and so was the 1st, and so on. So, obviously, it&#8217;s not a good idea to go with a for loop &#8211; they just don&#8217;t work.</p>
<p>Thankfully, there is a way to have the HybridDictionary behave normally, but it comes at a performance cost that probably destroys the value of this type of collection in many common uses. You&#8217;d just call the <a href="http://msdn.microsoft.com/en-us/library/system.collections.specialized.hybriddictionary.copyto.aspx">CopyTo</a> method, which will copy the dictionary to a one-dimensional, zero-based Array. That&#8217;s not all that intuitive, and it&#8217;s a trap I bet a lot of people fall into when starting to work with the HybridDictionary.</p>
<p>Given all this mess, I&#8217;m not a big fan of the HybridDictionary &#8211; give me a generic list any day!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ferretarmy.com/2008/07/31/hybriddictionaries-careful/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

