Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. Iffalse, the dictionary is case-sensitive.
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.
