ภาพรวม
HybridDictionary มีคอนสตรัคเตอร์ new HybridDictionary(bool caseInsensitive) ที่รับพารามิเตอร์บูลีน: true = case-insensitive (ค่าเริ่มต้น), false = case-sensitive
ตัวอย่างที่ 1: สร้างแบบ Case-Sensitive (caseInsensitive = false)
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
HybridDictionary dict = new HybridDictionary(false); // case-sensitive
dict.Add("A", "AB");
dict.Add("B", "BC");
dict.Add("C", "DE");
dict.Add("D", "de");
Console.WriteLine("Key/Value pairs...");
foreach(DictionaryEntry de in dict)
Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
}
}
ผลลัพธ์
Key/Value pairs...
Key = A, Value = AB
Key = B, Value = BC
Key = C, Value = DE
Key = D, Value = de
ตัวอย่างที่ 2: สร้างแบบ Case-Insensitive (caseInsensitive = true)
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
HybridDictionary dict = new HybridDictionary(true); // case-insensitive
dict.Add("A", "AB");
dict.Add("B", "BC");
dict.Add("C", "DE");
dict.Add("D", "de");
dict.Add("e", "FG");
dict.Add("F", "gh");
Console.WriteLine("Key/Value pairs...");
foreach(DictionaryEntry de in dict)
Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
}
}
ผลลัพธ์
Key/Value pairs...
Key = A, Value = AB
Key = B, Value = BC
Key = C, Value = DE
Key = D, Value = de
Key = e, Value = FG
Key = F, Value = gh
หมายเหตุ: ตัวอย่างที่ 2 ในต้นฉบับใช้ caseInsensitive = true แต่ผลลัพธ์แสดง D และ d แยกกัน ซึ่งขัดแย้งกับพฤติกรรมจริง (ควรทับกัน) โค้ดด้านบนแสดงพฤติกรรมจริง
คำอธิบายเพิ่มเติม
caseInsensitive = true: ใช้ StringComparer.OrdinalIgnoreCase คีย์ "D" และ "d" ถือว่าเหมือนกันcaseInsensitive = false: ใช้ StringComparer.Ordinal คีย์ "D" และ "d" ต่างกัน- คอนสตรัคเตอร์เพิ่มเติม:
new HybridDictionary(int initialSize, bool caseInsensitive)ระบุขนาดเริ่มต้นและ case sensitivity พร้อมกัน - HybridDictionary เหมาะสำหรับขนาดข้อมูลที่ไม่แน่ใจ จะปรับโครงสร้างภายในอัตโนมัติ