What is HybridDictionary in .Net

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:              

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

Thanks.

Ebook Download
View all
Learn
View all