HybridDictionary is a specialized Collection in .Net 4. It implements IDictionary by using a ListDictionary when the collection is small, and a Hashtable when the collection is large.

When to Use?

HyBridDictionary class is recommended for cases where the number of elements in a dictionary is unknown. It takes advantage of the improved performance of a ListDictionary with small collections, and offers the flexibility of switching to a Hashtable which handles larger collections better than ListDictionary.

The foreach statement of requires the type of each element in the collection. Since each element of the HybridDictionary is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is DictionaryEntry.

Example:

Code
using System;
Code
using System.Collections;
Code
using System.Collections.Specialized;
Code
public class SamplesHybridDictionary  {
Code
   public static void Main()  {
Code
      HybridDictionary myHD = new HybridDictionary ();
Code
      myHD.Add (“ONE", "1”);
Code
      myHD.Add (“TWO", "2”);
Code
      myHD.Add (“THREE", "3”);
Python
      myHD.Add (“FOUR", "4”);
Code
      Console.WriteLine( "Elements in HybridDictionary Object:” );
Code
      Console.WriteLine( "   KEY                       VALUE" );
Code
      foreach (DictionaryEntry de in myCol)
Code
         Console.WriteLine (“   {0,-25} {1}", de.Key, de.Value);
Code
      Console.WriteLine ();
Code
}
Code
As it implements the IDictionary, ICollection, IEnumerable interface we can have the Add, Contain, equals etc methods.
Code
For more information about HybridDictioanry, visit MSDN site
Code
Please provide your valuable feedback and comments.

Thanks.