Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C#에서 ArrayList가 읽기 전용인지 확인하는 방법

C#에서는 IsReadOnly 속성을 사용하여 ArrayList가 읽기 전용인지 여부를 간단하게 확인할 수 있습니다. 이 속성은 현재 컬렉션에 요소를 추가, 수정 또는 삭제할 수 있는지를 나타내며, 읽기 전용인 경우 true, 그렇지 않으면 false를 반환합니다.

예제 1

다음은 ArrayList가 읽기 전용인지 확인하는 코드입니다.

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

위 예제에서 ArrayList.Synchronized() 메서드로 감싼 리스트는 스레드 동기화가 적용되어 True가 반환되고, ArrayList.FixedSize()로 생성된 리스트는 고정 크기이므로 역시 True가 반환됩니다. 반면 원본 리스트인 list1은 요소 추가 및 수정이 자유롭기 때문에 IsReadOnly 값이 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는 기본적으로 동기화되지 않고 읽기 전용도 아니기 때문에 두 속성 모두 False가 반환됩니다. 만약 읽기 전용 리스트가 필요하다면 ArrayList.ReadOnly() 메서드를 사용하여 기존 리스트를 감싸면 되며, 이 경우 IsReadOnly 속성은 true를 반환하게 됩니다.