Creating an empty case-sensitive HybridDictionary Class in C#

The HybridDictionary class in C# is a collection that combines the performance benefits of both ListDictionary and Hashtable. It automatically switches between these implementations based on the number of elements. For small collections, it uses ListDictionary, and for larger collections, it switches to Hashtable for better performance.

When creating a HybridDictionary, you can specify whether it should be case-sensitive or case-insensitive through its constructor parameter.

Syntax

Following is the syntax for creating a case-sensitive HybridDictionary −

HybridDictionary dict = new HybridDictionary(false);

Following is the syntax for creating a case-insensitive HybridDictionary −

HybridDictionary dict = new HybridDictionary(true);

Parameters

The HybridDictionary constructor accepts a boolean parameter −

  • caseInsensitive − If true, the dictionary is case-insensitive. If false, the dictionary is case-sensitive.

HybridDictionary Case Sensitivity Case-Sensitive (false) "A" ? "a" Keys "A" and "a" are treated as different entries Case-Insensitive (true) "A" = "a" Keys "A" and "a" are treated as the same entry

Using Case-Sensitive HybridDictionary

Example

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

public class Demo {
   public static void Main() {
      HybridDictionary dict = new HybridDictionary(false);
      dict.Add("A", "AB");
      dict.Add("B", "BC");
      dict.Add("C", "DE");
      dict.Add("D", "de");
      Console.WriteLine("Key/Value pairs...");
      foreach(DictionaryEntry de in dict)
         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 = de

Using Case-Insensitive HybridDictionary

Example

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

public class Demo {
   public static void Main() {
      HybridDictionary dict = new HybridDictionary(true);
      dict.Add("A", "AB");
      dict.Add("B", "BC");
      dict.Add("C", "DE");
      dict.Add("D", "de");
      dict.Add("e", "FG");
      dict.Add("F", "gh");
      Console.WriteLine("Key/Value pairs...");
      foreach(DictionaryEntry de in dict)
         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 = de
Key = e, Value = FG
Key = F, Value = gh

Comparison

Case-Sensitive (false) Case-Insensitive (true)
Keys "A" and "a" are different entries Keys "A" and "a" are treated as the same entry
More precise key matching Flexible key matching regardless of case
Default behavior when parameter is false Useful for user-friendly applications

Conclusion

HybridDictionary in C# provides an efficient collection that automatically optimizes performance based on size. The case sensitivity parameter allows you to control whether keys should be matched exactly (case-sensitive) or regardless of letter case (case-insensitive), making it suitable for various application scenarios.

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

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements