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

C# HybridDictionary 열거자 GetEnumerator() 사용법 – 키-값 쌍 반복하기

C#의 HybridDictionary는 저장된 요소 수에 따라 내부적으로 ListDictionary와 Hashtable을 자동으로 전환해 사용하는 특수한 컬렉션입니다. 이 컬렉션의 모든 요소를 순회하려면 GetEnumerator() 메서드를 사용해 열거자(enumerator)를 얻으면 됩니다.

GetEnumerator() 메서드 개요

GetEnumerator() 메서드는 HybridDictionary 전체를 반복할 수 있는 IDictionaryEnumerator 객체를 반환합니다. 반환된 열거자는 다음 멤버들을 통해 활용할 수 있습니다.

  • MoveNext(): 다음 요소로 이동합니다. 더 이상 요소가 없으면 false를 반환합니다.
  • Key: 현재 위치한 요소의 키를 반환합니다.
  • Value: 현재 위치한 요소의 값을 반환합니다.

예제 1: 두 개의 HybridDictionary 순회하기

아래 예제에서는 첫 번째 딕셔너리는 foreach 문으로, 두 번째 딕셔너리는 GetEnumerator()로 얻은 열거자로 각각 순회합니다.

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary dict1 = new HybridDictionary();
      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("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      HybridDictionary dict2 = new HybridDictionary();
      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("
HybridDictionary2 key-value pairs...");
      IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
   }
}

두 방식 모두 동일하게 딕셔너리에 저장된 모든 키-값 쌍을 출력합니다. 단순 순회에는 foreach가 편리하지만, 열거자를 직접 제어해야 하는 상황에서는 GetEnumerator()가 유용합니다.

출력 결과

HybridDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
HybridDictionary2 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

예제 2: 문자열 데이터가 담긴 HybridDictionary 순회하기

이번에는 이름 데이터를 저장한 HybridDictionary를 열거자로 순회해 보겠습니다.

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary dict = new HybridDictionary();
      dict.Add("A", "Gary");
      dict.Add("B", "Andy");
      dict.Add("C", "Mark");
      dict.Add("D", "Barry");
      dict.Add("E", "Katie");
      dict.Add("F", "John");
      Console.WriteLine("HybridDictionary key-value pairs...");
      IDictionaryEnumerator demoEnum = dict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
   }
}

출력 결과

HybridDictionary key-value pairs...
Key = A, Value = Gary
Key = B, Value = Andy
Key = C, Value = Mark
Key = D, Value = Barry
Key = E, Value = Katie
Key = F, Value = John

정리

HybridDictionary의 요소를 반복 처리하려면 GetEnumerator() 메서드로 IDictionaryEnumerator를 얻은 뒤, MoveNext()Key/Value 속성을 조합해 사용하면 됩니다. 간단한 순회라면 foreach 문과 DictionaryEntry를 사용하는 것도 좋은 대안이며, 두 방식 모두 컬렉션의 모든 항목을 안전하게 읽어올 수 있습니다.