Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use a HybridDictionary over other Dictionary types?

I am looking at the Collection classes in MSDN for the .Net framework. I ran into the HybridDictionary and it states (http://msdn.microsoft.com/en-us/library/system.collections.specialized.hybriddictionary.aspx):

Implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large.

So I wondered about the ListDictionary which states (http://msdn.microsoft.com/en-us/library/system.collections.specialized.listdictionary.aspx)

Recommended for collections that typically include fewer than 10 items.

Now that seems like an arbitrary number (of items) to me. I can't find in the documentation what the mechanism behind this would be, I suspected the boundary of performance would have been related to a number of items like 2^N (2 to the power of N).

Now I do use the collection type of Dictionary often, and the collections might contain 10 to 30 items, 50 tops, depending on the 'page size'.

But HybridDictionary and ListDictionary requires unboxing and there are no generic type contructors for them.

I can't find a comparison anywhere about the performance of a HybridDictionary vs Dictionary.

So when to actually use this HybridDictionary over other Dictonary types?

P.S. And if HybridDictionary switches to ListDictionary or HashTable when the number of items grow to optimize its functioning. Why ever use a ListDictionary? If some requirements in the software change, and suddenly a maximum of 20 items must be put in the ListDictionary, instead of a maximum number of 10 items, the code must be re-factored to HybridDictionary to maintain performance?

like image 232
Mike de Klerk Avatar asked Sep 13 '13 06:09

Mike de Klerk


People also ask

Why do we use the Dictionary class instead of the Hashtable class?

In Dictionary, you can store key/value pairs of same type. In Hashtable, there is no need to specify the type of the key and value. In Dictionary, you must specify the type of key and value. The data retrieval is slower than Dictionary due to boxing/ unboxing.

Which is faster Dictionary or Hashtable?

Dictionary is faster than hashtable as dictionary is a generic strong type. Hashtable is slower as it takes object as data type which leads to boxing and unboxing.

What is Hybrid Dictionary in C#?

HybridDictionary attempts to optimize Hashtable. It implements a linked list and hash table data structure. It implements IDictionary by using a ListDictionary when the collection is small, and a Hashtable when the collection is large.

Does C# Dictionary use hash table?

@BrianJ: Both HashTable (class) and Dictionary (class) are hash tables (concept), but a HashTable is not a Dictionary , nor is a Dictionary a HashTable .


1 Answers

When to use a HybridDictionary over other Dictionary types?

You would use the ListDictionary when you are certain the collection size will be less than 10 items.

The HybridDictionary is pretty much the same as Dictionary but will take advantage of the performance of ListDictionary when the collection size is less that 10 items. Once the collection grows above 10 the HybridDictionary will switch from using ListDictionary internally to using a HashTable like an ordinary Dictionary.

So when to use one, well if your collection is usually under 10 items but at times could grow larger, then HybridDictionary will be the one to use.

For example, we use HybridDictionary in our mobile device applications comms layer, the comms message queue will pretty much always be under 10 items, but if there is a backend server outage the comms messages will build up into the 100's or 1000's depending on how long the server is down, a ListDictionary in this scenario would be awful, and in that case HybridDictionary will switch to a HashTable to keep performance up and still give us maximum performance when it is under 10.

So its used in specialized places, hence the namespace it belongs to System.Collections.Specialized :)

like image 199
sa_ddam213 Avatar answered Oct 20 '22 11:10

sa_ddam213