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

C# StringDictionary에서 특정 키의 존재 여부를 확인하는 방법

C#에서 StringDictionary 컬렉션에 특정 키가 포함되어 있는지 확인하려면 ContainsKey() 메서드를 사용합니다. 이 메서드는 지정한 키가 StringDictionary에 존재하면 true를, 존재하지 않으면 false를 반환합니다.

StringDictionary란?

StringDictionary는 키와 값이 모두 문자열인 해시 테이블 형태의 컬렉션으로, System.Collections.Specialized 네임스페이스에 정의되어 있습니다. 참고로 StringDictionary는 내부적으로 대소문자를 구분하지 않으므로, 저장된 키 "G"는 조회 시 소문자 "g"로 출력될 수 있습니다.

예제 1: ContainsKey() 메서드 사용하기

다음 예제에서는 두 개의 StringDictionary를 생성하고, ContainsKey() 메서드로 특정 키의 존재 여부를 확인하는 방법을 보여줍니다.

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      Console.WriteLine("StringDictionary1 요소...");
      foreach(DictionaryEntry d in strDict1){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("StringDictionary1에 키 G가 있습니까? " + strDict1.ContainsKey("G"));
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("A", "John");
      strDict2.Add("B", "Andy");
      strDict2.Add("C", "Tim");
      strDict2.Add("D", "Ryan");
      strDict2.Add("E", "Kevin");
      strDict2.Add("F", "Katie");
      strDict2.Add("G", "Brad");
      Console.WriteLine("\nStringDictionary2 요소...");
      foreach(DictionaryEntry d in strDict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("\nDictionary2는 Dictionary1과 같습니까? = " + strDict2.Equals(strDict1));
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

StringDictionary1 요소...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary1에 키 G가 있습니까? True
StringDictionary2 요소...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Dictionary2는 Dictionary1과 같습니까? = False

출력 결과에서 볼 수 있듯이, strDict1.ContainsKey("G")는 키 "G"가 존재하기 때문에 True를 반환했습니다. 또한 두 딕셔너리의 내용이 동일해도 Equals() 메서드는 참조 비교를 수행하므로 False가 반환된 점에 유의하세요.

예제 2: 다른 키로 확인하기

이번에는 두 번째 StringDictionary에서 다른 키 "B"의 존재 여부를 추가로 확인하는 예제입니다.

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      Console.WriteLine("StringDictionary1 요소...");
      foreach(DictionaryEntry d in strDict1){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("StringDictionary1에 키 G가 있습니까? " + strDict1.ContainsKey("G"));
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("A", "John");
      strDict2.Add("B", "Andy");
      strDict2.Add("C", "Tim");
      strDict2.Add("D", "Ryan");
      strDict2.Add("E", "Kevin");
      strDict2.Add("F", "Katie");
      strDict2.Add("G", "Brad");
      Console.WriteLine("\nStringDictionary2 요소...");
      foreach(DictionaryEntry d in strDict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("\nDictionary2는 Dictionary1과 같습니까? = " + strDict2.Equals(strDict1));
      Console.WriteLine("StringDictionary2에 키 B가 있습니까? " + strDict2.ContainsKey("B"));
   }
}

출력 결과

실행 결과는 다음과 같습니다.

StringDictionary1 요소...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary1에 키 G가 있습니까? True
StringDictionary2 요소...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Dictionary2는 Dictionary1과 같습니까? = False
StringDictionary2에 키 B가 있습니까? True

정리

StringDictionary에서 특정 키의 존재 여부를 확인할 때는 ContainsKey() 메서드가 가장 간단하고 효율적인 방법입니다. 키가 존재하는지 미리 확인한 후 값을 조회하면, 존재하지 않는 키로 인한 오류를 방지할 수 있습니다.