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

Kiểm tra HybridDictionary có chứa key cụ thể trong C#

Giới thiệu

HybridDictionary (namespace System.Collections.Specialized) tự động chuyển từ **ListDictionary** (n < 10) sang **Hashtable** (n >= 10) để tối ưu hiệu năng. Phương thức Contains(object key) kiểm tra key tồn tại.

Ví dụ 1: HybridDictionary nhỏ (dùng ListDictionary nội bộ)

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

public class Demo
{
    public static void Main()
    {
        HybridDictionary dict = new HybridDictionary(5); // Initial size hint
        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}");

        Console.WriteLine($"\nContains key 'C': {dict.Contains("C")}"); // True
        Console.WriteLine($"Contains key 'Z': {dict.Contains("Z")}"); // False
    }
}

Kết quả

Key/Value pairs:
Key = A, Value = AB
Key = B, Value = BC
Key = C, Value = DE
Key = D, Value = FG
Key = E, Value = HI

Contains key 'C': True
Contains key 'Z': False

Ví dụ 2: Hai HybridDictionary, gán tham chiếu, Clear

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("Dict1:");
        foreach (DictionaryEntry d in dict1) Console.WriteLine($"{d.Key} {d.Value}");
        Console.WriteLine($"Count: {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("\nDict2:");
        foreach (DictionaryEntry d in dict2) Console.WriteLine($"{d.Key} {d.Value}");
        Console.WriteLine($"Count: {dict2.Count}");

        // Gán tham chiếu (KHÔNG COPY)
        HybridDictionary dict3 = dict2;
        Console.WriteLine($"\ndict3 == dict2? {dict3.Equals(dict2)}"); // True

        dict3.Clear();
        Console.WriteLine($"Dict2 Count sau dict3.Clear(): {dict2.Count}"); // 0 !!
        Console.WriteLine($"Dict3 Count: {dict3.Count}"); // 0
    }
}

Kết quả

Dict1:
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count: 6

Dict2:
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Count: 6

dict3 == dict2? True
Dict2 Count sau dict3.Clear(): 0
Dict3 Count: 0

Ví dụ 3: HybridDictionary lớn (tự chuyển sang Hashtable)

using System;
using System.Collections.Specialized;

public class Demo
{
    public static void Main()
    {
        HybridDictionary dict = new HybridDictionary();
        for (int i = 0; i < 15; i++)
            dict.Add(i.ToString(), $"Value {i}");

        Console.WriteLine($"Count: {dict.Count}"); // 15 -> dùng Hashtable nội bộ
        Console.WriteLine($"Contains '10': {dict.Contains("10")}"); // True
        Console.WriteLine($"Contains '20': {dict.Contains("20")}"); // False
    }
}

Đặc điểm HybridDictionary

  • Non-generic (key/value là object).
  • Key **không phân biệt chữ hoa/thường** (case-insensitive) mặc định.
  • Tự động chuyển ListDictionary -> Hashtable khi Count >= 10 (hoặc constructor size hint).
  • Không thread-safe. Dùng Hashtable.Synchronized wrapper nếu cần.
  • **Legacy**: Không dùng cho code mới. Thay bằng Dictionary<string, object> hoặc ConcurrentDictionary.

So sánh Contains


// HybridDictionary
bool has = dict.Contains("key"); // object key

// Dictionary (generic)
bool has = dict.ContainsKey("key"); // type-safe