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

Tạo HybridDictionary rỗng với độ nhạy cảm chữ hoa/thường trong C#

Giới thiệu

HybridDictionary (namespace System.Collections.Specialized) có constructor cho phép chỉ định caseInsensitive (bool). true = không phân biệt chữ hoa/thường (mặc định), false = phân biệt chữ hoa/thường.

Ví dụ 1: HybridDictionary mặc định (case-insensitive)

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

public class Demo
{
    public static void Main()
    {
        HybridDictionary myDict = new HybridDictionary(); // caseInsensitive = true (mặc định)
        myDict.Add("A", "AB");
        myDict.Add("B", "BC");
        myDict.Add("C", "DE");
        myDict.Add("D", "FG");
        myDict.Add("e", "fg"); // 'e' khác 'E' nhưng case-insensitive → xung đột nếu có 'E'
        
        Console.WriteLine("Key/Value pairs...");
        foreach (DictionaryEntry de in myDict)
        {
            Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
        }
    }
}

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 = fg

Ví dụ 2: HybridDictionary case-insensitive (mặc định) với khóa trùng chữ hoa/thường

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

public class Demo
{
    public static void Main()
    {
        HybridDictionary myDict = new HybridDictionary(); // caseInsensitive = true
        myDict.Add("A", "PQ");
        myDict.Add("B", "BC");
        myDict.Add("C", "tu");
        myDict.Add("D", "FG");
        myDict.Add("d", "gh"); // 'd' và 'D' coi như trùng khóa
        myDict.Add("e", "fg");
        
        Console.WriteLine("Key/Value pairs...");
        foreach (DictionaryEntry de in myDict)
        {
            Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
        }
    }
}

Kết quả

Key/Value pairs...
Key = A, Value = PQ
Key = B, Value = BC
Key = C, Value = tu
Key = D, Value = FG
Key = d, Value = gh
Key = e, Value = fg

Lưu ý quan trọng về case-insensitive

Trong ví dụ 2, khóa "D" và "d" được coi là TRÙNG KHÓA khi caseInsensitive=true. Tuy nhiên kết quả output hiển thị cả hai - điều này có thể do hành vi triển khai cụ thể hoặc ví dụ gốc có lỗi. Thực tế:

  • Nếu caseInsensitive=true: Add("D", ...) rồi Add("d", ...) sẽ ném ArgumentException (khóa trùng)
  • Nếu caseInsensitive=false: "D" và "d" là hai khóa khác nhau

Constructor HybridDictionary

// Mặc định case-insensitive
HybridDictionary dict1 = new HybridDictionary();
HybridDictionary dict2 = new HybridDictionary(true);  // Explicit case-insensitive

// Case-sensitive
HybridDictionary dict3 = new HybridDictionary(false);

// Với dung lượng ban đầu
HybridDictionary dict4 = new HybridDictionary(10, true);  // capacity, caseInsensitive
HybridDictionary dict5 = new HybridDictionary(10, false);

So sánh HybridDictionary vs Dictionary<TKey, TValue>

Luôn hash table
Đặc điểm HybridDictionary Dictionary<TKey, TValue>
Case sensitivity Cấu hình được Mặc định case-sensitive (có thể custom IEqualityComparer)
Kiểu khóa/giá trị object TKey, TValue (generic)
Hiệu suất nhỏ ListDictionary (O(n))
Hiệu suất lớn Hashtable (O(1)) O(1)
Ngưỡng chuyển đổi ~9-10 phần tử N/A