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

ตรวจสอบว่า ArrayList เป็น Read-Only หรือไม่ใน C#

ภาพรวม

คุณสมบัติ ArrayList.IsReadOnly เสมอคืนค่า false สำหรับ ArrayList ทั่วไป เพราะ ArrayList อนุญาตให้เพิ่ม ลบ แก้ไขได้เสมอ ยกเว้นกรณีที่ห่อหุ้มด้วย ArrayList.ReadOnly()

ตัวอย่างที่ 1: ตรวจสอบ IsReadOnly, IsFixedSize, IsSynchronized

using System;
using System.Collections;

public class Demo
{
    public static void Main(String[] args)
    {
        ArrayList list1 = new ArrayList();
        list1.Add("ABC");
        list1.Add("BCD");
        list1.Add("CDE");
        list1.Add("DEF");
        list1.Add("EFG");
        list1.Add("GHI");
        list1.Add("HIJ");
        list1.Add("IJK");
        list1.Add("JKL");
        list1.Add("KLM");
        Console.WriteLine("Elements in ArrayList...");
        foreach (string res in list1)
        {
            Console.WriteLine(res);
        }
        ArrayList list = ArrayList.Synchronized(list1);
        Console.WriteLine("Is ArrayList synchronized? = " + list.IsSynchronized);
        ArrayList list2 = ArrayList.FixedSize(list1);
        Console.WriteLine("Is ArrayList have a fixed size? = " + list2.IsFixedSize);
        Console.WriteLine("Is the ArrayList read-only? = " + list1.IsReadOnly);
    }
}

ผลลัพธ์

Elements in ArrayList...
ABC
BCD
CDE
DEF
EFG
GHI
HIJ
IJK
JKL
KLM
Is ArrayList synchronized? = True
Is ArrayList have a fixed size? = True
Is the ArrayList read-only? = False

ตัวอย่างที่ 2: ตรวจสอบ ArrayList ทั่วไปสองตัว

using System;
using System.Collections;

public class Demo
{
    public static void Main(String[] args)
    {
        ArrayList list1 = new ArrayList();
        list1.Add("A");
        list1.Add("B");
        list1.Add("C");
        list1.Add("D");
        list1.Add("E");
        list1.Add("F");
        Console.WriteLine("Elements in ArrayList1...");
        foreach (string res in list1)
        {
            Console.WriteLine(res);
        }
        Console.WriteLine("Is ArrayList1 synchronized? = " + list1.IsSynchronized);
        Console.WriteLine("Is the ArrayList1 read-only? = " + list1.IsReadOnly);
        ArrayList list2 = new ArrayList();
        list2.Add("A");
        list2.Add("B");
        list2.Add("C");
        list2.Add("D");
        list2.Add("E");
        list2.Add("F");
        Console.WriteLine("Elements in ArrayList2...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }
        Console.WriteLine("Is ArrayList synchronized? = " + list2.IsSynchronized);
        Console.WriteLine("Is the ArrayList2 read-only? = " + list2.IsReadOnly);
    }
}

ผลลัพธ์

Elements in ArrayList1...
A
B
C
D
E
F
Is ArrayList1 synchronized? = False
Is the ArrayList1 read-only? = False
Elements in ArrayList2...
A
B
C
D
E
F
Is ArrayList synchronized? = False
Is the ArrayList2 read-only? = False

คำอธิบายเพิ่มเติม

  • ArrayList.IsReadOnly เสมอ false เว้นแต่สร้างด้วย ArrayList.ReadOnly(list)
  • ArrayList.FixedSize(list) คืนค่า wrapper ที่มี IsFixedSize = true (ไม่สามารถเพิ่ม/ลบได้ แก้ไขค่าที่มีอยู่ได้)
  • ArrayList.Synchronized(list) คืนค่า wrapper ที่ thread-safe (IsSynchronized = true)
  • ArrayList เป็น legacy ใช้ List<T> แทนสำหรับโค้ดใหม่
  • List<T> ไม่มี IsReadOnly/IsFixedSize/IsSynchronized properties โดยตรง