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 HybridDictionary with specified initial size in C#
The HybridDictionary class in C# is a special collection that automatically switches between a ListDictionary for small collections and a Hashtable for larger ones. When creating a HybridDictionary with a specified initial size, you can optimize performance by indicating the expected number of elements.
The HybridDictionary uses a ListDictionary when the collection is small (typically less than 10 items) and switches to a Hashtable when it grows larger, providing the best performance characteristics for both scenarios.
Syntax
Following is the syntax for creating a HybridDictionary with specified initial size −
HybridDictionary dict = new HybridDictionary(initialSize);
Where initialSize is an integer indicating the approximate number of entries the HybridDictionary can initially contain.
How It Works
The HybridDictionary automatically chooses the most efficient internal data structure based on the initial size parameter:
If initial size is small (? 6), it starts with a
ListDictionaryIf initial size is larger, it directly uses a
HashtableIt can switch from
ListDictionarytoHashtableas items are added
Using HybridDictionary with Small Initial Size
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
HybridDictionary dict = new HybridDictionary(5);
dict.Add("A", "AB");
dict.Add("B", "BC");
dict.Add("C", "DE");
dict.Add("D", "FG");
dict.Add("E", "HI");
Console.WriteLine("Key/Value pairs...");
foreach(DictionaryEntry d in dict)
Console.WriteLine("Key = "+d.Key + ", Value = " + d.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 = HI
Using HybridDictionary with Large Initial Size
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
HybridDictionary dict = new HybridDictionary(10);
dict.Add("1", 100);
dict.Add("2", 200);
dict.Add("3", 300);
dict.Add("4", 400);
dict.Add("5", 500);
dict.Add("6", 600);
dict.Add("7", 700);
dict.Add("8", 800);
dict.Add("9", 900);
dict.Add("10", 1000);
Console.WriteLine("Key/Value pairs...");
foreach(DictionaryEntry d in dict)
Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
}
}
The output of the above code is −
Key/Value pairs... Key = 10, Value = 1000 Key = 1, Value = 100 Key = 2, Value = 200 Key = 3, Value = 300 Key = 4, Value = 400 Key = 5, Value = 500 Key = 6, Value = 600 Key = 7, Value = 700 Key = 8, Value = 800 Key = 9, Value = 900
Comparison with Other Collections
| Collection Type | Best For | Performance |
|---|---|---|
| ListDictionary | Small collections (? 10 items) | O(n) lookup |
| Hashtable | Large collections | O(1) average lookup |
| HybridDictionary | Variable-size collections | Optimal for both scenarios |
Conclusion
The HybridDictionary with specified initial size provides automatic performance optimization by choosing the best internal data structure. It starts with a ListDictionary for small collections and switches to a Hashtable for larger ones, making it ideal when the collection size varies or is unknown at compile time.
