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
| Comparer | Case-Sensitive | Culture-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) ช่วยประสิทธิภาพเมื่อทราบจำนวนข้อมูลประมาณ