คุณสมบัติ IsSynchronized ของ ListDictionary คืนค่า bool บ่งชี้ว่าการเข้าถึงพจนานุกรมปลอดภัยต่อเธรด (Thread-Safe) หรือไม่ โดยค่าเริ่มต้นคือ false
ตัวอย่างที่ 1: ตรวจสอบคุณสมบัติต่างๆ
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
ListDictionary dict = new ListDictionary();
dict.Add("1", "SUV");
dict.Add("2", "Sedan");
dict.Add("3", "Utility Vehicle");
dict.Add("4", "Compact Car");
dict.Add("5", "SUV");
dict.Add("6", "Sedan");
dict.Add("7", "Utility Vehicle");
dict.Add("8", "Compact Car");
dict.Add("9", "Crossover");
dict.Add("10", "Electric Car");
Console.WriteLine("ListDictionary องค์ประกอบ...");
foreach (DictionaryEntry d in dict)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nListDictionary มีขนาดคงที่? = " + dict.IsFixedSize);
Console.WriteLine("ListDictionary เป็น Read-Only? = " + dict.IsReadOnly);
Console.WriteLine("ListDictionary ถูกซิงโครไนซ์? = " + dict.IsSynchronized);
Console.WriteLine("ListDictionary มีคีย์ K? = " + dict.Contains("K"));
Console.WriteLine("ListDictionary มีคีย์ 9? = " + dict.Contains("9"));
}
}
ผลลัพธ์
ListDictionary องค์ประกอบ... 1 SUV 2 Sedan 3 Utility Vehicle 4 Compact Car 5 SUV 6 Sedan 7 Utility Vehicle 8 Compact Car 9 Crossover 10 Electric Car ListDictionary มีขนาดคงที่? = False ListDictionary เป็น Read-Only? = False ListDictionary ถูกซิงโครไนซ์? = False ListDictionary มีคีย์ K? = False ListDictionary มีคีย์ 9? = True
ตัวอย่างที่ 2: ListDictionary สองตัว พร้อม Enumerator
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
ListDictionary dict1 = new ListDictionary();
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("ListDictionary1 องค์ประกอบ...");
foreach (DictionaryEntry d in dict1)
{
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict2 = new ListDictionary();
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("\nListDictionary2 คู่คีย์-ค่า...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("ListDictionary2 มีขนาดคงที่? = " + dict2.IsFixedSize);
Console.WriteLine("ListDictionary2 เป็น Read-Only? = " + dict2.IsReadOnly);
Console.WriteLine("ListDictionary2 ถูกซิงโครไนซ์? = " + dict2.IsSynchronized);
Console.WriteLine("ListDictionary2 มีคีย์ 5? = " + dict2.Contains("5"));
}
}
ผลลัพธ์
ListDictionary1 องค์ประกอบ... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear ListDictionary2 คู่คีย์-ค่า... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six ListDictionary2 มีขนาดคงที่? = False ListDictionary2 เป็น Read-Only? = False ListDictionary2 ถูกซิงโครไนซ์? = False ListDictionary2 มีคีย์ 5? = True
ตัวอย่างที่ 3: เหมือนตัวอย่างที่ 1 (ซ้ำ)
// เนื้อหาเหมือนตัวอย่างที่ 1 ข้ามไป
คุณสมบัติสำคัญของ ListDictionary สรุป
| คุณสมบัติ | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|
IsSynchronized | false | ไม่ปลอดภัยต่อเธรด (Not Thread-Safe) |
IsReadOnly | false | สามารถเพิ่ม/ลบ/แก้ไขได้ |
IsFixedSize | false | ขนาดเปลี่ยนแปลงได้ |
วิธีทำให้ Thread-Safe
// 1. ใช้ lock ด้วยตัวเอง
private readonly object _lock = new object();
lock (_lock) { dict.Add("key", "value"); }
// 2. ใช้ ConcurrentDictionary<TKey, TValue> (แนะนำสำหรับ .NET 2.0+)
using System.Collections.Concurrent;
var concurrentDict = new ConcurrentDictionary<string, string>();
concurrentDict.TryAdd("key", "value"); // Thread-Safe โดยอัตโนมัติ
// 3. Legacy: ใช้ Hashtable.Synchronized (แต่ ListDictionary ไม่มีเมธอด Synchronized)
ข้อสังเกต
ListDictionaryไม่ได้รับการซิงโครไนซ์ (IsSynchronized = false)- เหมาะสำหรับข้อมูลน้อย (< 10 รายการ) ใช้ Linked List
- คลาส Legacy ใน
System.Collections.Specializedแนะนำใช้Dictionary<TKey, TValue>หรือConcurrentDictionary<TKey, TValue>แทน