Creating an empty HybridDictionary with specified case sensitivity in C#

The HybridDictionary class in C# is a collection that combines the benefits of both ListDictionary and Hashtable. It automatically switches from a list-based implementation to a hash table when the collection grows beyond a certain size. By default, HybridDictionary is case-sensitive, but you can specify case sensitivity behavior during initialization.

Syntax

Following is the syntax for creating an empty HybridDictionary with default case sensitivity −

HybridDictionary dictionary = new HybridDictionary();

Following is the syntax for creating an empty HybridDictionary with specified case sensitivity −

HybridDictionary dictionary = new HybridDictionary(bool caseInsensitive);

Parameters

  • caseInsensitive − A boolean value that specifies whether the HybridDictionary is case-insensitive. If true, the dictionary ignores case; if false or omitted, it is case-sensitive.

HybridDictionary Case Sensitivity Case Sensitive (default) new HybridDictionary() "A" and "a" are different dict["A"] ? dict["a"] Both can coexist Case Insensitive new HybridDictionary(true) "A" and "a" are same dict["A"] = dict["a"] Overwrites existing Choose based on your application's key comparison requirements

Using Default Case-Sensitive HybridDictionary

The default constructor creates a case-sensitive HybridDictionary where keys "A" and "a" are treated as different −

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

public class Demo {
   public static void Main() {
      HybridDictionary myDict = new HybridDictionary();
      myDict.Add("A", "AB");
      myDict.Add("B", "BC");
      myDict.Add("C", "DE");
      myDict.Add("D", "FG");
      myDict.Add("e", "fg");
      
      Console.WriteLine("Key/Value pairs...");
      foreach(DictionaryEntry de in myDict)
         Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
   }
}

The output of the above code is −

Key/Value pairs...
Key = A, Value = AB
Key = B, Value = BC
Key = C, Value = DE
Key = D, Value = FG
Key = e, Value = fg

Using Case-Sensitive HybridDictionary with Mixed Case Keys

This example demonstrates that case-sensitive HybridDictionary allows both "D" and "d" as separate keys −

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

public class Demo {
   public static void Main() {
      HybridDictionary myDict = new HybridDictionary();
      myDict.Add("A", "PQ");
      myDict.Add("B", "BC");
      myDict.Add("C", "tu");
      myDict.Add("D", "FG");
      myDict.Add("d", "gh");
      myDict.Add("e", "fg");
      
      Console.WriteLine("Key/Value pairs...");
      foreach(DictionaryEntry de in myDict)
         Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
   }
}

The output of the above code is −

Key/Value pairs...
Key = A, Value = PQ
Key = B, Value = BC
Key = C, Value = tu
Key = D, Value = FG
Key = d, Value = gh
Key = e, Value = fg

Using Case-Insensitive HybridDictionary

When creating a case-insensitive HybridDictionary, keys are compared without regard to case −

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

public class Demo {
   public static void Main() {
      HybridDictionary myDict = new HybridDictionary(true);
      myDict.Add("Name", "John");
      myDict.Add("Age", "25");
      myDict.Add("City", "New York");
      
      Console.WriteLine("Case-insensitive dictionary:");
      Console.WriteLine("Value for 'name': " + myDict["name"]);
      Console.WriteLine("Value for 'NAME': " + myDict["NAME"]);
      Console.WriteLine("Value for 'Name': " + myDict["Name"]);
      
      Console.WriteLine("\nAll entries:");
      foreach(DictionaryEntry de in myDict)
         Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
   }
}

The output of the above code is −

Case-insensitive dictionary:
Value for 'name': John
Value for 'NAME': John
Value for 'Name': John

All entries:
Key = Name, Value = John
Key = Age, Value = 25
Key = City, Value = New York

Conclusion

HybridDictionary in C# provides efficient storage that automatically switches between ListDictionary and Hashtable implementations. By default, it is case-sensitive, but you can create a case-insensitive version by passing true to the constructor, making it suitable for scenarios where key case should be ignored.

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

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements