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

C# StringDictionary에서 지정된 키를 가진 항목 제거하는 방법

C#의 StringDictionary 컬렉션에서 특정 키를 가진 항목을 제거하려면 Remove() 메서드를 사용합니다. 이 메서드는 매개변수로 전달된 키와 일치하는 키-값 쌍을 컬렉션에서 삭제하며, 해당 키가 존재하지 않더라도 예외를 발생시키지 않고 그대로 통과합니다.

또한 StringDictionary는 키의 대소문자를 구분하지 않기 때문에, 저장 시 모든 키가 소문자로 변환되어 출력되는 점도 참고하면 좋습니다.

예제 1: 단일 키 제거하기

다음 예제에서는 여러 개의 키-값 쌍을 추가한 후, Remove() 메서드로 하나의 항목을 제거하는 과정을 보여줍니다.

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 key-value pairs...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
      strDict1.Remove("D");
         Console.WriteLine("
StringDictionary1 key-value pairs after removing a key...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("1", "A");
      strDict2.Add("2", "B");
      strDict2.Add("3", "C");
      strDict2.Add("4", "D");
      strDict2.Add("5", "E");
      Console.WriteLine("
StringDictionary2 key-value pairs...");
      IEnumerator demoEnum = strDict2.GetEnumerator();
      DictionaryEntry d;
      while (demoEnum.MoveNext()) {
         d = (DictionaryEntry)demoEnum.Current;
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }
   }
}

출력 결과

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

StringDictionary1 key-value pairs...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad

StringDictionary1 key-value pairs after removing a key...
a John
b Andy
c Tim
e Kevin
f Katie
g Brad

StringDictionary2 key-value pairs...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E

실행 결과를 보면 Remove("D") 호출 이후 키 "d"에 해당하는 항목(Ryan)이 목록에서 사라진 것을 확인할 수 있습니다. 또한 두 번째 StringDictionary에서는 GetEnumerator() 메서드와 IEnumerator를 사용하여 키-값 쌍을 순회하는 방법도 함께 보여주고 있습니다.

예제 2: 여러 개의 키 동시에 제거하기

Remove() 메서드를 연속으로 호출하면 한 번에 여러 항목을 제거할 수도 있습니다.

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "One");
      strDict1.Add("B", "Two");
      strDict1.Add("C", "Three");
      strDict1.Add("D", "Four");
      strDict1.Add("E", "Five");
      Console.WriteLine("StringDictionary1 key-value pairs...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
      strDict1.Remove("B");
      strDict1.Remove("C");
      Console.WriteLine("
StringDictionary1 key-value pairs after removing a key...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
   }
}

출력 결과

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

StringDictionary1 key-value pairs...
a One
b Two
c Three
d Four
e Five

StringDictionary1 key-value pairs after removing a key...
a One
d Four
e Five

이처럼 Remove() 메서드를 활용하면 StringDictionary에서 원하는 키의 항목을 손쉽게 삭제할 수 있습니다. 대소문자 구분 없이 키가 처리되므로, "B"를 제거하더라도 실제로는 소문자 "b"로 저장된 항목이 정상적으로 삭제됩니다.