C#의 ListDictionary 컬렉션이 읽기 전용(Read-Only) 상태인지 확인하려면 IsReadOnly 속성을 사용하면 됩니다. 이 속성은 해당 컬렉션에 요소를 추가하거나 수정할 수 있는지 여부를 Boolean 값으로 반환하여, 런타임에 컬렉션의 변경 가능 여부를 판단하는 데 유용합니다.
ListDictionary의 주요 속성 정리
본문 예제에서 사용되는 핵심 속성과 메서드는 다음과 같습니다.
- IsReadOnly – ListDictionary가 읽기 전용이면
true, 그렇지 않으면false를 반환합니다. - IsFixedSize – 컬렉션의 크기가 고정되어 있으면
true를 반환합니다. - IsSynchronized – 컬렉션이 스레드로부터 안전하게 동기화되어 있으면
true를 반환합니다. - Contains(key) – 지정한 키가 컬렉션 내에 존재하는지 여부를 확인합니다.
예제 1: 두 개의 ListDictionary 속성 확인
아래 예제에서는 두 개의 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 elements...");
foreach(DictionaryEntry d in dict1){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Is the ListDictionary1 having fixed size? = "+dict1.IsFixedSize);
Console.WriteLine("If ListDictionary1 read-only? = "+dict1.IsReadOnly);
Console.WriteLine("Is ListDictionary1 synchronized = "+dict1.IsSynchronized);
Console.WriteLine("The ListDictionary1 has the key 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 key-value pairs...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Is the ListDictionary2 having fixed size? = "+dict2.IsFixedSize);
Console.WriteLine("If ListDictionary2 read-only? = "+dict2.IsReadOnly);
Console.WriteLine("Is ListDictionary2 synchronized = "+dict2.IsSynchronized);
Console.WriteLine("The ListDictionary2 has the key 5? = "+dict2.Contains("5"));
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
ListDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Is the ListDictionary1 having fixed size? = False If ListDictionary1 read-only? = False Is ListDictionary1 synchronized = False The ListDictionary1 has the key M? = False ListDictionary2 key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Is the ListDictionary2 having fixed size? = False If ListDictionary2 read-only? = False Is ListDictionary2 synchronized = False The ListDictionary2 has the key 5? = True
실행 결과를 보면 두 ListDictionary 모두 IsReadOnly가 False로 출력됩니다. 즉, 일반적인 방식으로 생성된 ListDictionary는 기본적으로 읽기/쓰기가 모두 가능한 컬렉션입니다. 또한 존재하지 않는 키 "M"은 false를, 실제로 추가된 키 "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 elements...");
foreach(DictionaryEntry d in dict){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nIs the ListDictionary having fixed size? = "+dict.IsFixedSize);
Console.WriteLine("If ListDictionary read-only? = "+dict.IsReadOnly);
Console.WriteLine("Is ListDictionary synchronized = "+dict.IsSynchronized);
Console.WriteLine("The ListDictionary has the key K? = "+dict.Contains("K"));
Console.WriteLine("The ListDictionary has the key 9? = "+dict.Contains("9"));
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
ListDictionary elements... 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 Is the ListDictionary having fixed size? = False If ListDictionary read-only? = False Is ListDictionary synchronized = False The ListDictionary has the key K? = False The ListDictionary has the key 9? = True
정리
ListDictionary는 System.Collections.Specialized 네임스페이스에 포함된 컬렉션으로, 소량의 데이터를 저장할 때 내부적으로 연결 리스트를 사용하여 효율적으로 동작합니다. IsReadOnly 속성을 활용하면 컬렉션이 읽기 전용 상태인지 손쉽게 확인할 수 있으며, 일반적으로 직접 생성한 ListDictionary는 읽기 전용이 아닌 false를 반환합니다. 반면 읽기 전용 래퍼로 감싼 경우에는 true가 반환되므로, 컬렉션을 수정하기 전에 이 속성을 검사하면 안전한 코드 작성에 도움이 됩니다.