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

C#에서 ListDictionary가 고정 크기(Fixed Size)인지 확인하는 방법

ListDictionary는 System.Collections.Specialized 네임스페이스에 속한 컬렉션으로, 키와 값 쌍을 단일 연결 리스트 형태로 저장합니다. 이 글에서는 ListDictionary의 IsFixedSize 속성을 사용하여 해당 컬렉션이 고정 크기인지 동적으로 크기가 변하는지 확인하는 방법을 알아보겠습니다.

IsFixedSize 속성은 컬렉션의 크기가 고정되어 있으면 true를, 요소를 자유롭게 추가하거나 제거할 수 있으면 false를 반환합니다. 일반적인 ListDictionary 인스턴스는 항목 추가에 따라 크기가 늘어나는 동적 컬렉션이므로 항상 false를 반환합니다.

예제 1

다음은 두 개의 ListDictionary를 생성하고 IsFixedSize 속성과 함께 IsReadOnly, IsSynchronized, Contains 메서드도 함께 확인하는 예제입니다.

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는 키 "M"을 포함하지 않아 Contains 메서드가 false를 반환했고, 두 번째 ListDictionary는 키 "5"를 포함하고 있어 true를 반환했습니다. 또한 두 컬렉션 모두 IsFixedSize, IsReadOnly, IsSynchronized 속성이 모두 false로 나타납니다. 이는 ListDictionary가 기본적으로 크기가 고정되지 않고, 읽기 전용이 아니며, 스레드로부터 안전하지 않음을 의미합니다.

예제 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

핵심 정리

이번 예제들을 통해 다음 사항을 확인할 수 있습니다.

IsFixedSize: ListDictionary는 동적으로 크기가 조절되는 컬렉션이므로 항상 false를 반환합니다.
Contains: 지정한 키가 컬렉션에 존재하는지 여부를 bool 값으로 반환합니다.
IsReadOnly / IsSynchronized: 기본 ListDictionary는 읽기 전용이 아니며 스레드 동기화도 지원하지 않습니다. 스레드 안전한 컬렉션이 필요하다면 별도의 래퍼(wrapper)를 사용해야 합니다.