Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

สร้าง HybridDictionary แบบ Case-Sensitive ด้วยขนาดเริ่มต้นใน C#

Constructor HybridDictionary(int initialSize, bool caseInsensitive) อนุญาตให้ระบุทั้งขนาดเริ่มต้นและว่าคีย์ควรเป็น Case-Sensitive (false) หรือ Case-Insensitive (true)

ตัวอย่างที่ 1: Case-Sensitive (caseInsensitive = false)

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

public class Demo
{
    public static void Main()
    {
        // false = Case-Sensitive (แยกตัวพิมพ์เล็ก/ใหญ่)
        HybridDictionary myDict = new HybridDictionary(5, false);
        myDict.Add("A", "AB");
        myDict.Add("B", "BC");
        myDict.Add("C", "DE");
        myDict.Add("D", "FG");
        myDict.Add("e", "fg"); // 'e' ตัวเล็ก ต่างจาก 'E' ตัวใหญ่

        Console.WriteLine("คู่คีย์/ค่า (Case-Sensitive)...");
        foreach (DictionaryEntry de in myDict)
        {
            Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
        }
    }
}

ผลลัพธ์ (เก็บ 'D' และ 'e' แยกกันได้)

คู่คีย์/ค่า (Case-Sensitive)...
Key = A, Value = AB
Key = B, Value = BC
Key = C, Value = DE
Key = D, Value = FG
Key = e, Value = fg

ตัวอย่างที่ 2: Case-Insensitive (caseInsensitive = true) - จะเกิด Error

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

public class Demo
{
    public static void Main()
    {
        // true = Case-Insensitive (ไม่แยกตัวพิมพ์เล็ก/ใหญ่)
        HybridDictionary myDict = new HybridDictionary(5, true);
        myDict.Add("A", "AB");
        myDict.Add("B", "BC");
        myDict.Add("C", "DE");
        myDict.Add("e", "fg"); // 'e' ตัวเล็ก
        myDict.Add("E", "FG"); // 'E' ตัวใหญ่ -> ชนกับ 'e'!

        Console.WriteLine("คู่คีย์/ค่า (Case-Insensitive)...");
        foreach (DictionaryEntry de in myDict)
        {
            Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
        }
    }
}

ผลลัพธ์ (Runtime Exception)

Run-time exception: An entry with the same key already exists.
Stack Trace: 
[System.ArgumentException: An entry with the same key already exists.]
   at System.Collections.Specialized.ListDictionary.Add(Object key, Object value)
   at System.Collections.Specialized.HybridDictionary.Add(Object key, Object value)
   at Demo.Main() :line 15

คำอธิบาย

  • caseInsensitive = true: ใช้ CaseInsensitiveComparer (หรือ StringComparer.OrdinalIgnoreCase) คีย์ "e" และ "E" ถือว่าเหมือนกัน
  • caseInsensitive = false (Default): ใช้ Object.Equals / GetHashCode ปกติ คีย์ "e" และ "E" ต่างกัน
  • เมื่อพยายาม Add คีย์ซ้ำ (ตามกฎ Comparer) จะโยน ArgumentException

วิธีทำ Case-Insensitive ใน Dictionary สมัยใหม่ (แนะนำ)

// Dictionary Generic + StringComparer (แนะนำ)
var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
dict["Key"] = "Value1";
dict["KEY"] = "Value2"; // แทนที่ Key เดิม
dict["key"] = "Value3"; // แทนที่อีกครั้ง

Console.WriteLine(dict.Count); // 1
Console.WriteLine(dict["key"]); // Value3

// ตรวจสอบคีย์ (Case-Insensitive)
Console.WriteLine(dict.ContainsKey("KEY")); // True
Console.WriteLine(dict.TryGetValue("Key", out var val)); // True, val = "Value3"

เปรียบเทียบ StringComparer

ComparerCase-SensitiveCulture-Awareใช้เมื่อไหร่
Ordinalใช่ไม่Key, ID, Code, Internal (เร็วที่สุด)
OrdinalIgnoreCaseไม่ไม่User Input, Case-Insensitive Key
CurrentCultureใช่ใช่UI, Sorting ตามภาษา
CurrentCultureIgnoreCaseไม่ใช่UI Search, Display
InvariantCultureใช่ใช่ (Invariant)File, Config, Cross-Culture
InvariantCultureIgnoreCaseไม่ใช่ (Invariant)Case-Insensitive Cross-Culture

ข้อสังเกต

  • HybridDictionary เป็น Legacy Class แนะนำใช้ Dictionary<TKey, TValue> พร้อม StringComparer แทน
  • Case-Insensitive ใน HybridDictionary ใช้ Comparer แบบเก่า (CaseInsensitiveComparer) ซึ่งอาจแตกต่างจาก StringComparer.OrdinalIgnoreCase ในรายละเอียด
  • ขนาดเริ่มต้น (initialSize) ช่วยประสิทธิภาพเมื่อทราบจำนวนข้อมูลประมาณ