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

C# StringDictionary에서 모든 항목 제거하기 – Clear() 메서드 완벽 가이드

C#에서 StringDictionary에 저장된 모든 항목(키/값 쌍)을 한 번에 제거하려면 Clear() 메서드를 사용하면 됩니다. 이 메서드는 컬렉션 내의 모든 요소를 즉시 삭제하며, 호출 이후 Count 속성 값은 0으로 변경됩니다. 아래 예제를 통해 실제 동작 방식을 살펴보겠습니다.

예제

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 elements...");
      foreach(DictionaryEntry d in strDict1){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Does StringDictionary1 has key 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 elements...");
      foreach(DictionaryEntry d in strDict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of key/value pairs in StringDictionary2 = " + strDict2.Count);
      strDict2.Clear();
      Console.WriteLine("Count of key/value pairs in StringDictionary2 (updated) = " + strDict2.Count);
   }
}

출력 결과

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

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Does StringDictionary1 has key G? True
StringDictionary2 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Count of key/value pairs in StringDictionary2 = 7
Count of key/value pairs in StringDictionary2 (updated) = 0

실행 결과를 보면 Clear() 메서드 호출 전에는 StringDictionary2에 7개의 키/값 쌍이 저장되어 있었지만, 호출 직후 항목 개수가 0으로 업데이트된 것을 확인할 수 있습니다.

참고로 출력 결과에서 키가 모두 소문자(a, b, c…)로 표시되는 이유는 StringDictionary가 기본적으로 키를 소문자로 변환하여 처리하기 때문입니다.

다른 예제

이번에는 좀 더 간결한 예제로 Clear() 메서드의 동작을 다시 확인해 보겠습니다 −

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      StringDictionary strDict = new StringDictionary();
      strDict.Add("A", "John");
      strDict.Add("B", "Andy");
      strDict.Add("C", "Tim");
      strDict.Add("D", "Ryan");
      Console.WriteLine("StringDictionary elements...");
      foreach(DictionaryEntry d in strDict){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of key/value pairs in StringDictionary = " + strDict.Count);
      strDict.Clear();
      Console.WriteLine("Count of key/value pairs in StringDictionary (updated) = " + strDict.Count);
   }
}

출력 결과

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

StringDictionary elements...
a John
b Andy
c Tim
d Ryan
Count of key/value pairs in StringDictionary = 4
Count of key/value pairs in StringDictionary (updated) = 0

4개의 항목을 가진 StringDictionary에서 Clear() 메서드를 호출한 후, 전체 항목이 삭제되고 개수가 0으로 초기화되었음을 알 수 있습니다. 이처럼 Clear() 메서드는 컬렉션을 비울 때 가장 간단하고 효율적인 방법입니다.