Lớp HybridDictionary trong namespace System.Collections.Specialized cung cấp thuộc tính IsReadOnly để kiểm tra xem từ điển có chỉ đọc hay không. Thuộc tính này luôn trả về false vì HybridDictionary không hỗ trợ chế độ chỉ đọc.
Ví dụ 1: Kiểm tra các thuộc tính của 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);
}
Console.WriteLine("HybridDictionary1 có kích thước cố định? = " + dict1.IsFixedSize);
Console.WriteLine("HybridDictionary1 chỉ đọc? = " + dict1.IsReadOnly);
Console.WriteLine("HybridDictionary1 đồng bộ? = " + dict1.IsSynchronized);
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 phần tử của HybridDictionary2...");
foreach (DictionaryEntry d in dict2)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("HybridDictionary1 bằng HybridDictionary2? = " + dict1.Equals(dict2));
Console.WriteLine("HybridDictionary2 có kích thước cố định? = " + dict2.IsFixedSize);
Console.WriteLine("HybridDictionary2 chỉ đọc? = " + dict2.IsReadOnly);
Console.WriteLine("HybridDictionary2 đồng bộ? = " + dict2.IsSynchronized);
}
}
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 HybridDictionary1 có kích thước cố định? = False HybridDictionary1 chỉ đọc? = False HybridDictionary1 đồng bộ? = False Các phần tử của HybridDictionary2... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six HybridDictionary1 bằng HybridDictionary2? = False HybridDictionary2 có kích thước cố định? = False HybridDictionary2 chỉ đọc? = False HybridDictionary2 đồng bộ? = False
Ví dụ 2: Kiểm tra đơ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("1", "Finance");
dict.Add("2", "Marketing");
dict.Add("3", "IT");
dict.Add("4", "Operations");
dict.Add("5", "Purchase");
Console.WriteLine("Các phần tử của HybridDictionary...");
foreach (DictionaryEntry d in dict)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("HybridDictionary có kích thước cố định? = " + dict.IsFixedSize);
Console.WriteLine("HybridDictionary chỉ đọc? = " + dict.IsReadOnly);
Console.WriteLine("HybridDictionary đồng bộ? = " + dict.IsSynchronized);
}
}
Kết quả
Các phần tử của HybridDictionary... 1 Finance 2 Marketing 3 IT 4 Operations 5 Purchase HybridDictionary có kích thước cố định? = False HybridDictionary chỉ đọc? = False HybridDictionary đồng bộ? = False
Giải thích các thuộc tính
| Thuộc tính | Kiểu trả về | Mô tả |
|---|---|---|
IsReadOnly |
bool |
Luôn trả về false. HybridDictionary không hỗ trợ chế độ chỉ đọc. |
IsFixedSize |
bool |
Luôn trả về false. Kích thước có thể thay đổi. |
IsSynchronized |
bool |
Luôn trả về false. Không an toàn cho luồng (thread-safe). |
Lưu ý
- HybridDictionary tự động chuyển đổi giữa
ListDictionary(khi nhỏ) vàHashtable(khi lớn) để tối ưu hiệu năng - Để có từ điển chỉ đọc, hãy dùng
ReadOnlyDictionary<TKey, TValue>hoặc wrapper tùy chỉnh - HybridDictionary đã lỗi thời, khuyến nghị dùng
Dictionary<TKey, TValue>cho code mới