HybridDictionary Class in C#?

The HybridDictionary class implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large. This provides optimal performance for both small and large collections by leveraging the strengths of each underlying data structure.

The HybridDictionary automatically switches from ListDictionary to Hashtable when the number of elements exceeds a certain threshold (typically around 10 items), making it ideal for scenarios where the collection size is unpredictable.

HybridDictionary Internal Structure Small Collection ListDictionary Linear search ? ~10 items Large Collection Hashtable Hash-based lookup > ~10 items Auto switch

Key Properties

Property Description
Count Gets the number of key/value pairs contained in the HybridDictionary.
IsFixedSize Gets a value indicating whether the HybridDictionary has a fixed size.
IsReadOnly Gets a value indicating whether the HybridDictionary is read-only.
IsSynchronized Gets a value indicating whether the HybridDictionary is synchronized (thread safe).
Item[Object] Gets or sets the value associated with the specified key.
Keys Gets an ICollection containing the keys in the HybridDictionary.
Values Gets an ICollection containing the values in the HybridDictionary.

Common Methods

Method Description
Add(Object, Object) Adds an entry with the specified key and value into the HybridDictionary.
Clear() Removes all entries from the HybridDictionary.
Contains(Object) Determines whether the HybridDictionary contains a specific key.
CopyTo(Array, Int32) Copies the HybridDictionary entries to a one-dimensional Array instance at the specified index.
GetEnumerator() Returns an IDictionaryEnumerator that iterates through the HybridDictionary.
Remove(Object) Removes the entry with the specified key from the HybridDictionary.

Using HybridDictionary with Count Property

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      HybridDictionary dict1 = new HybridDictionary();
      dict1.Add("A", "SUV");
      dict1.Add("B", "MUV");
      dict1.Add("C", "AUV");
      Console.WriteLine("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of Key/value pairs in Dictionary1 = " + dict1.Count);
      
      HybridDictionary dict2 = new HybridDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      dict2.Add("4", "Four");
      dict2.Add("5", "Five");
      dict2.Add("6", "Six");
      Console.WriteLine("\nHybridDictionary2 elements...");
      foreach(DictionaryEntry d in dict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of Key/value pairs in Dictionary2 = " + dict2.Count);
      dict2.Clear();
      Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = " + dict2.Count);
   }
}

The output of the above code is −

HybridDictionary1 elements...
A SUV
B MUV
C AUV
Count of Key/value pairs in Dictionary1 = 3

HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Count of Key/value pairs in Dictionary2 = 6
Count of Key/value pairs in Dictionary2 (Updated) = 0

Checking HybridDictionary Properties

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      HybridDictionary dict1 = new HybridDictionary();
      dict1.Add("A", "Books");
      dict1.Add("B", "Electronics");
      dict1.Add("C", "Smart Wearables");
      dict1.Add("D", "Pet Supplies");
      dict1.Add("E", "Clothing");
      dict1.Add("F", "Footwear");
      
      Console.WriteLine("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Is the HybridDictionary1 having fixed size? = " + dict1.IsFixedSize);
      Console.WriteLine("If HybridDictionary1 read-only? = " + dict1.IsReadOnly);
      Console.WriteLine("Is HybridDictionary1 synchronized = " + dict1.IsSynchronized);
      
      HybridDictionary dict2 = new HybridDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      
      Console.WriteLine("\nHybridDictionary2 elements...");
      foreach(DictionaryEntry d in dict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Is HybridDictionary1 equal to HybridDictionary2? = " + (dict1.Equals(dict2)));
      Console.WriteLine("Is the HybridDictionary2 having fixed size? = " + dict2.IsFixedSize);
      Console.WriteLine("If HybridDictionary2 read-only? = " + dict2.IsReadOnly);
      Console.WriteLine("Is HybridDictionary2 synchronized = " + dict2.IsSynchronized);
   }
}

The output of the above code is −

HybridDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Is the HybridDictionary1 having fixed size? = False
If HybridDictionary1 read-only? = False
Is HybridDictionary1 synchronized = False

HybridDictionary2 elements...
1 One
2 Two
3 Three
Is HybridDictionary1 equal to HybridDictionary2? = False
Is the HybridDictionary2 having fixed size? = False
If HybridDictionary2 read-only? = False
Is HybridDictionary2 synchronized = False

Checking for Key Existence

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      HybridDictionary dict = new HybridDictionary();
      dict.Add("Name", "John");
      dict.Add("Age", "25");
      dict.Add("City", "New York");
      
      Console.WriteLine("Dictionary contains:");
      Console.WriteLine("Name key exists: " + dict.Contains("Name"));
      Console.WriteLine("Age key exists: " + dict.Contains("Age"));
      Console.WriteLine("Country key exists: " + dict.Contains("Country"));
      
      Console.WriteLine("\nAccessing values:");
      if(dict.Contains("Name")) {
         Console.WriteLine("Name: " + dict["Name"]);
      }
      if(dict.Contains("Country")) {
         Console.WriteLine("Country: " + dict["Country"]);
      } else {
         Console.WriteLine("Country key not found");
      }
   }
}

The output of the above code is −

Dictionary contains:
Name key exists: True
Age key exists: True
Country key exists: False

Accessing values:
Name: John
Country key not found

Conclusion

The HybridDictionary class provides an optimal solution for collections of unpredictable size by automatically switching between ListDictionary and Hashtable based on the number of elements. It combines the efficiency of linear search for small collections with the performance of hash-based lookup for larger ones, making it ideal for general-purpose dictionary operations.

Updated on: 2026-03-17T07:04:36+05:30

250 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements