Dictionaries in C#: The HybridDictionary

So far we’ve looked at the HashTable collection, and determined that it’s the way to go for large collections. We’ve also taken a look at the ListDictionary, which is geared towards very small collections. But what if you don’t know how big your collection will be?

There are many situations where at design time you simply can not know what the final size of your collection will be. Sometimes it will depend on user input, what range of data the user selects for example. Other times it may be dependant on the data, how much is out there for you to use. There’s enough uncertainty in all our lives to get stressed out, so to keep us calm Microsoft has provided the HybridDictionary collection.

A HybridDictionary will self monitor itself. Initially, and while it remains small the HybridDictionary will internally keep itself as a ListDictionary. Once it exceeds a certain threshold, it will then convert it’s data into a HashTable. The nice part is you don’t have to care when, or what state it is currently in. All you have to know is the HybridDictionary is managing the memory most efficiently on your behalf.

Like it’s sibling the ListDictionary, the HybridDictionary will require a using System.Collections.Specialized reference. After that, it has the same interface as the HashTable and can be treated as such.

      HybridDictionary genders = new HybridDictionary();

 

      genders.Add(“M”, “Male”);

      genders.Add(“F”, “Female”);

 

The question many will ask “well why not just use a HybridDictionary all the time?” As with most things in life, there is a trade off. There is a some overhead associated with HybridDictionaries, as they have to constantly monitor their own size, then convert back and forth between ListDictionaries and HashTables as needed. In return, you get the best possible performance from your collection as it changes in size.

The moral of the story, when you know you have a small collection, use a ListDictionary. On the other hand, if you know with certainty your collection will be large, use a HashTable. But for all those other times when you just don’t know, the HybridDictionary is your answer.

About these ads

2 Responses to “Dictionaries in C#: The HybridDictionary”

  1. Danish Says:

    thanks, very very infromative, it makes me clear

  2. Recent Links Tagged With "dictionaries" - JabberTags Says:

    [...] wolfnini on Fri 02-1-2009 Win Cute Back-to-School Dictionaries Saved by tosh13 on Fri 02-1-2009 Comment on Dictionaries in C#: The HybridDictionary by Danish Saved by dvdmerwe on Sun 21-12-2008 Educational tools: spending money Saved by vilan95 on Tue [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 93 other followers

%d bloggers like this: