As we all know that Dcitinoary provide performance with some sacrifices/overhead on the space, normally the redundancy of space is needed because it has to decrease the amount of contention between different elemetns.
While HybridDictionary provides a compromise (actually it is not a compromise, it works in that way that if the size of elements is small, then it uses ListDictionary -- we will come to list dictionary later, but if thesize of elements are growing , then it will in turns uses HashTable...)
Let's see the code,
public class Class1 { public static void Main(string[] args) { HybridDictionary myCol = new HybridDictionary(); myCol.Add("Braeburn Apples", "1.49"); myCol.Add( "Fuji Apples", "1.29" ); myCol.Add( "Gala Apples", "1.49" ); myCol.Add( "Golden Delicious Apples", "1.29" ); myCol.Add( "Granny Smith Apples", "0.89" ); myCol.Add( "Red Delicious Apples", "0.99" ); myCol.Add( "Plantain Bananas", "1.49" ); myCol.Add( "Yellow Bananas", "0.79" ); myCol.Add( "Strawberries", "3.33" ); myCol.Add( "Cranberries", "5.98" ); myCol.Add( "Navel Oranges", "1.29" ); myCol.Add( "Grapes", "1.99" ); myCol.Add( "Honeydew Melon", "0.59" ); myCol.Add( "Seedless Watermelon", "0.49" ); myCol.Add( "Pineapple", "1.49" ); myCol.Add( "Nectarine", "1.99" ); myCol.Add( "Plums", "1.69" ); myCol.Add( "Peaches", "1.99" ); // Display the contents of the collection using foreach. This is the preferred method. Console.WriteLine( "Displays the elements using foreach:" ); PrintKeysAndValues1( myCol ); // Display the contents of the collection using the enumerator. Console.WriteLine( "Displays the elements using the IDictionaryEnumerator:" ); PrintKeysAndValues2( myCol ); // Display the contents of the collection using the Keys, Values, Count, and Item properties. Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" ); PrintKeysAndValues3( myCol ); // Copies the HybridDictionary to an array with DictionaryEntry elements. DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; myCol.CopyTo( myArr, 0 ); // Displays the values in the array. Console.WriteLine( "Displays the elements in the array:" ); Console.WriteLine( " KEY VALUE" ); for ( int i = 0; i < myArr.Length; i++ ) Console.WriteLine( " {0,-25} {1}", myArr[i].Key, myArr[i].Value ); Console.WriteLine(); // Searches for a key. if ( myCol.Contains( "Kiwis" ) ) Console.WriteLine( "The collection contains the key \"Kiwis\"." ); else Console.WriteLine( "The collection does not contain the key \"Kiwis\"." ); Console.WriteLine(); // Deletes a key. myCol.Remove( "Plums" ); Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" ); PrintKeysAndValues1( myCol ); // Clears the entire collection. myCol.Clear(); Console.WriteLine( "The collection contains the following elements after it is cleared:" ); PrintKeysAndValues1( myCol ); } public static void PrintKeysAndValues1(IDictionary myCol) { Console.WriteLine(" KEY VALUE"); foreach (DictionaryEntry de in myCol) { Console.WriteLine(" {0,-25} {1}", de.Key, de.Value ); } Console.WriteLine(); } public static void PrintKeysAndValues2(IDictionary myCol) { Console.WriteLine(" KEY VALUE"); IDictionaryEnumerator enumerator = myCol.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(" {0,-25} {1}", enumerator.Key, enumerator.Value); } Console.WriteLine(); } public static void PrintKeysAndValues3(IDictionary myCol) { String[] myKeys = new string[myCol.Count]; myCol.Keys.CopyTo(myKeys, 0); // you can also use the Array.Copy to, but this is much better way to use Console.WriteLine(" INDEX KEY VALUE"); for (int i = 0; i < myCol.Count; ++i) { Console.WriteLine(" {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]]); } Console.WriteLine(); } }
To bear in mind, you will use the HybridDictionary only when you feel there is a performance bottleneck and the mitigate risk of using a general dictionary for a small set of data or risk of using a ListDictionary for a large set of data.
You can find some more information about the use of HybridDictionary at HybridDictionary class