C#에서 ListDictionary 컬렉션에 특정 키가 포함되어 있는지 확인하려면 Contains() 메서드를 사용하면 됩니다. 이 메서드는 지정한 키가 ListDictionary 내에 존재하면 true를, 존재하지 않으면 false를 반환합니다.
아래 예제 코드를 통해 실제 동작 방식을 자세히 살펴보겠습니다.
예제 1
두 개의 ListDictionary를 생성하고, 두 번째 딕셔너리에 키 "5"가 존재하는지 확인하는 예제입니다.
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);
}
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("ListDictionary의 크기가 고정되어 있는가? = "+dict2.IsFixedSize);
Console.WriteLine("ListDictionary가 읽기 전용인가? = "+dict2.IsReadOnly);
Console.WriteLine("ListDictionary가 동기화되어 있는가? = "+dict2.IsSynchronized);
Console.WriteLine("ListDictionary에 키 5가 존재하는가? = "+dict2.Contains("5"));
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
ListDictionary1 요소들... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear 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 ListDictionary의 크기가 고정되어 있는가? = False ListDictionary가 읽기 전용인가? = False ListDictionary가 동기화되어 있는가? = False ListDictionary에 키 5가 존재하는가? = True
출력 결과에서 볼 수 있듯이 dict2.Contains("5")는 키 "5"가 실제로 존재하기 때문에 True를 반환했습니다.
예제 2
이번에는 존재하지 않는 키를 조회했을 때 어떤 결과가 나오는지 확인해 보겠습니다.
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가 읽기 전용인가? = "+dict.IsReadOnly);
Console.WriteLine("ListDictionary가 동기화되어 있는가? = "+dict.IsSynchronized);
Console.WriteLine("ListDictionary에 키 M이 존재하는가? = "+dict.Contains("M"));
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
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가 읽기 전용인가? = False ListDictionary가 동기화되어 있는가? = False ListDictionary에 키 M이 존재하는가? = False
키 "M"은 ListDictionary에 추가된 적이 없으므로 Contains() 메서드가 False를 반환한 것을 확인할 수 있습니다.
참고 사항
ListDictionary는 System.Collections.Specialized 네임스페이스에 속하며, 작은 컬렉션(일반적으로 10개 미만의 항목)에서 성능이 좋도록 설계된 단일 연결 리스트 기반의 컬렉션입니다. 또한 위 예제에서 확인할 수 있듯이 기본 상태에서는 크기가 고정되어 있지 않고, 읽기 전용이 아니며, 스레드 동기화도 지원하지 않습니다.