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

ตรวจสอบว่า ListDictionary เป็น Read-Only ใน C#

คุณสมบัติ IsReadOnly ของ ListDictionary คืนค่า bool บ่งชี้ว่าพจนานุกรมเป็นแบบอ่านอย่างเดียวหรือไม่ ค่าเริ่มต้นคือ false

ตัวอย่างที่ 1: ตรวจสอบคุณสมบัติของสอง ListDictionary

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);
        }

        Console.WriteLine("ListDictionary1 มีขนาดคงที่? = " + dict1.IsFixedSize);
        Console.WriteLine("ListDictionary1 เป็น Read-Only? = " + dict1.IsReadOnly);
        Console.WriteLine("ListDictionary1 ถูกซิงโครไนซ์? = " + dict1.IsSynchronized);
        Console.WriteLine("ListDictionary1 มีคีย์ M? = " + dict1.Contains("M"));

        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
ListDictionary1 มีขนาดคงที่? = False
ListDictionary1 เป็น Read-Only? = False
ListDictionary1 ถูกซิงโครไนซ์? = False
ListDictionary1 มีคีย์ M? = False
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

ตัวอย่างที่ 2: ListDictionary อื่น

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

สรุปคุณสมบัติทั้งสาม

คุณสมบัติค่าความหมาย
IsReadOnlyfalseสามารถแก้ไขได้ (Add/Remove/Update)
IsFixedSizefalseขนาดเปลี่ยนแปลงได้
IsSynchronizedfalseไม่ Thread-Safe ต้อง Lock เอง

วิธีสร้าง Read-Only Wrapper (Legacy)

// ไม่มีเมธอด ReadOnly ตรงๆ สำหรับ ListDictionary
// ต้องสร้าง Wrapper เอง หรือใช้ Dictionary แบบ Read-Only แทน

// แนะนำ: ใช้ IReadOnlyDictionary<TKey, TValue> (Interface)
IReadOnlyDictionary<string, string> readOnlyDict = new Dictionary<string, string> 
{ 
    { "A", "Books" }, 
    { "B", "Electronics" } 
};

// หรือ .NET 6+ มี FrozenDictionary (Immutable, High Performance)
var frozen = System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary(new Dictionary<string, string> 
{ 
    { "A", "Books" } 
});

ข้อสังเกต

  • ListDictionary เป็น Legacy class แนะนำใช้ Dictionary<TKey, TValue> แทน
  • IsReadOnly เสมอเป็น false สำหรับ ListDictionary ปกติ
  • หากต้องการ Read-Only จริง ให้ใช้ Interface IReadOnlyDictionary<TKey, TValue> หรือ FrozenDictionary