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 a Case-Sensitive HybridDictionary with specified initial size in C#
The HybridDictionary class in C# is a collection that combines the benefits of both ListDictionary and Hashtable. It automatically switches from a ListDictionary (for small collections) to a Hashtable (for larger collections) based on the number of elements. You can create a case-sensitive HybridDictionary with a specified initial size using its constructor.
Syntax
Following is the syntax for creating a HybridDictionary with specified initial size and case sensitivity −
HybridDictionary myDict = new HybridDictionary(initialSize, caseInsensitive);
Parameters
-
initialSize − The approximate number of entries that the HybridDictionary can initially contain.
-
caseInsensitive − A boolean value that specifies whether the HybridDictionary is case-insensitive. Set to
falsefor case-sensitive,truefor case-insensitive.
Creating a Case-Sensitive HybridDictionary
To create a case-sensitive HybridDictionary, set the second parameter to false. This allows keys like "A" and "a" to be treated as different keys −
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
HybridDictionary myDict = new HybridDictionary(5, false);
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, DE Key = D, Value = FG Key = e, Value = fg
Case-Insensitive vs Case-Sensitive Comparison
The following example demonstrates the difference between case-insensitive (true) and case-sensitive (false) HybridDictionary behavior −
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
Console.WriteLine("Case-Sensitive HybridDictionary:");
HybridDictionary caseSensitive = new HybridDictionary(5, false);
caseSensitive.Add("A", "Value1");
caseSensitive.Add("a", "Value2");
foreach(DictionaryEntry de in caseSensitive)
Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
Console.WriteLine("\nCase-Insensitive HybridDictionary:");
HybridDictionary caseInsensitive = new HybridDictionary(5, true);
caseInsensitive.Add("B", "Value3");
Console.WriteLine("Added key 'B' with value 'Value3'");
try {
caseInsensitive.Add("b", "Value4");
}
catch (ArgumentException ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Case-Sensitive HybridDictionary: Key = A, Value = Value1 Key = a, Value = Value2 Case-Insensitive HybridDictionary: Added key 'B' with value 'Value3' Error: An item with the same key has already been added.
How It Works
The HybridDictionary automatically chooses the underlying data structure based on performance:
-
For collections with 10 or fewer items, it uses a ListDictionary (linked list implementation).
-
For collections with more than 10 items, it switches to a Hashtable for better performance.
-
The
initialSizeparameter helps determine which implementation to start with.
Conclusion
The HybridDictionary with case sensitivity provides flexibility in key handling while optimizing performance for different collection sizes. Setting the case-sensitive parameter to false allows distinct keys that differ only in case, making it useful for scenarios where case matters in your key comparisons.
