Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Lớp HybridDictionary trong C#

Lớp HybridDictionary triển khai IDictionary bằng ListDictionary khi tập hợp nhỏ, và chuyển sang Hashtable khi tập hợp lớn.

Thuộc tính

STTThuộc tính & Mô tả
1Count: Số cặp khóa/giá trị.
2IsFixedSize: Kích thước cố định?
3IsReadOnly: Chỉ đọc?
4IsSynchronized: Đã đồng bộ?
5Item[Object]: Lấy/đặt giá trị theo khóa.
6Keys: Tập hợp khóa.
7SyncRoot: Đối tượng đồng bộ.
8Values: Tập hợp giá trị.

Phương thức

STTPhương thức & Mô tả
1Add(Object, Object): Thêm cặp khóa/giá trị.
2Clear(): Xóa tất cả.
3Contains(Object): Kiểm tra khóa.
4CopyTo(Array, Int32): Sao chép vào mảng.
5GetEnumerator(): Trả về IDictionaryEnumerator.

Ví dụ 1: Đếm cặp khóa/giá trị

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        HybridDictionary dict1 = new HybridDictionary();
        dict1.Add("A", "SUV");
        dict1.Add("B", "MUV");
        dict1.Add("C", "AUV");
        
        Console.WriteLine("HybridDictionary1...");
        foreach(DictionaryEntry d in dict1) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Số cặp khóa/giá trị Dict1 = " + dict1.Count);
        
        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("\nHybridDictionary2...");
        foreach(DictionaryEntry d in dict2) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Số cặp khóa/giá trị Dict2 = " + dict1.Count); // Lỗi logic: nên là dict2.Count
        dict2.Clear();
        Console.WriteLine("Số cặp khóa/giá trị Dict2 (cập nhật) = " + dict2.Count);
    }
}

Kết quả

HybridDictionary1...
A SUV
B MUV
C AUV
Số cặp khóa/giá trị Dict1 = 3
HybridDictionary2...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Số cặp khóa/giá trị Dict2 = 3
Số cặp khóa/giá trị Dict2 (cập nhật) = 0

Ví dụ 2: Kiểm tra thuộc tính

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("HybridDictionary1...");
        foreach(DictionaryEntry d in dict1) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("Kích thước cố định? = " + dict1.IsFixedSize);
        Console.WriteLine("Chỉ đọc? = " + dict1.IsReadOnly);
        Console.WriteLine("Đồ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("\nHybridDictionary2...");
        foreach(DictionaryEntry d in dict2) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("Dict1 bằng Dict2? = " + (dict1.Equals(dict2)));
        Console.WriteLine("Dict2 kích thước cố định? = " + dict2.IsFixedSize);
        Console.WriteLine("Dict2 chỉ đọc? = " + dict2.IsReadOnly);
        Console.WriteLine("Dict2 đồng bộ? = " + dict2.IsSynchronized);
    }
}

Kết quả

HybridDictionary1...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Kích thước cố định? = False
Chỉ đọc? = False
Đồng bộ? = False

HybridDictionary2...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Dict1 bằng Dict2? = False
Dict2 kích thước cố định? = False
Dict2 chỉ đọc? = False
Dict2 đồng bộ? = False