HybridDictionary kết hợp ListDictionary (cho tập dữ liệu nhỏ) và Hashtable (cho tập dữ liệu lớn) để tối ưu hiệu năng. Để duyệt qua các cặp key-value, sử dụng phương thức GetEnumerator() trả về IDictionaryEnumerator.
Ví dụ 1: Duyệt hai HybridDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
HybridDictionary dict1 = new HybridDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("Các phần tử của HybridDictionary1...");
foreach (DictionaryEntry d in dict1)
{
Console.WriteLine(d.Key + " " + d.Value);
}
HybridDictionary dict2 = new HybridDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nCác cặp key-value của HybridDictionary2...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
Kết quả
Các phần tử của HybridDictionary1... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Các cặp key-value của HybridDictionary2... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six
Ví dụ 2: Duyệt HybridDictionary đơn giản
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
HybridDictionary dict = new HybridDictionary();
dict.Add("A", "Gary");
dict.Add("B", "Andy");
dict.Add("C", "Mark");
dict.Add("D", "Barry");
dict.Add("E", "Katie");
dict.Add("F", "John");
Console.WriteLine("Các cặp key-value của HybridDictionary...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
Kết quả
Các cặp key-value của HybridDictionary... Key = A, Value = Gary Key = B, Value = Andy Key = C, Value = Mark Key = D, Value = Barry Key = E, Value = Katie Key = F, Value = John
Cách hiện đại: Sử dụng foreach
HybridDictionary dict = new HybridDictionary();
dict.Add("A", "Gary");
dict.Add("B", "Andy");
// ...
// Cách 1: DictionaryEntry (cũ)
foreach (DictionaryEntry entry in dict)
{
Console.WriteLine($"Key = {entry.Key}, Value = {entry.Value}");
}
// Cách 2: Var type inference
foreach (var entry in dict)
{
Console.WriteLine($"Key = {entry.Key}, Value = {entry.Value}");
}
// Cách 3: Chỉ lấy keys hoặc values
foreach (var key in dict.Keys) { ... }
foreach (var value in dict.Values) { ... }
Đặc điểm của HybridDictionary
| Đặc điểm | Mô tả |
|---|---|
| Cấu trúc | ListDictionary (≤ 9 phần tử) → Hashtable (> 9 phần tử) |
| Chuyển đổi | Tự động khi vượt ngưỡng |
| Thứ tự | Không bảo đảm (dựa trên Hashtable khi lớn) |
| Hiệu năng | Tốt cho cả collection nhỏ và lớn |
| Namespace | System.Collections.Specialized |
Lưu ý quan trọng
- Đã lỗi thời - Microsoft khuyến nghị dùng
Dictionary<TKey, TValue>cho code mới - Không type-safe (key và value là
object) - Không an toàn cho đa luồng
- Thứ tự duyệt không đảm bảo là thứ tự thêm vào khi số lượng lớn
- Dùng
OrderedDictionarynếu cần giữ thứ tự
So sánh với Dictionary<TKey, TValue>
// Dictionary<TKey, TValue> - Type-safe, hiệu năng tốt hơn
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("A", "Gary");
// Duyệt (khuyến nghị)
foreach (var kvp in dict)
{
Console.WriteLine($"Key = {kvp.Key}, Value = {kvp.Value}");
}
// Deconstruction (C# 7+)
foreach (var (key, value) in dict) { ... }