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

C# HybridDictionary에 지정된 키와 값 추가하기

C#에서 HybridDictionary에 지정된 키와 값을 추가하려면 Add() 메서드를 사용합니다. HybridDictionary는 컬렉션의 크기가 작을 때는 ListDictionary로, 커지면 Hashtable로 내부적으로 전환되는 특수한 컬렉션으로, 요소 개수가 유동적인 상황에서 효율적인 성능을 제공합니다.

아래 예제에서는 Add() 메서드로 키-값 쌍을 삽입하고, IsFixedSize, IsReadOnly 속성으로 컬렉션의 특성을 확인해 보겠습니다.

예제

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary dict = new HybridDictionary();
      dict.Add("A", "Bags");
      dict.Add("B", "Electronics");
      dict.Add("C", "Smart Wearables");
      dict.Add("D", "Pet Supplies");
      Console.WriteLine("HybridDictionary elements...");
      foreach(DictionaryEntry d in dict){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Is the HybridDictionary having fixed size? = "+dict.IsFixedSize);
      Console.WriteLine("Is HybridDictionary read-only? = "+dict.IsReadOnly);
   }
}

출력 결과

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

HybridDictionary elements...
A Bags
B Electronics
C Smart Wearables
D Pet Supplies
Is the HybridDictionary having fixed size? = False
Is HybridDictionary read-only? = False

실행 결과를 보면 HybridDictionary의 크기는 고정되어 있지 않고(False) 읽기 전용도 아니므로(False), 언제든지 새로운 요소를 자유롭게 추가할 수 있습니다.

두 번째 예제

이번에는 두 개의 HybridDictionary를 만들어 요소를 추가한 뒤, Count 속성으로 키-값 쌍의 개수를 확인하고 Clear() 메서드로 전체 요소를 삭제하는 방법을 살펴보겠습니다.

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);
      }
      Console.WriteLine("Count of Key/value pairs in Dictionary1 = "+dict1.Count);
      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 elements...");
      foreach(DictionaryEntry d in dict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of Key/value pairs in Dictionary2 = "+dict2.Count);
      dict2.Clear();
      Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = "+dict2.Count);
   }
}

출력 결과

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

HybridDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of Key/value pairs in Dictionary1 = 6

HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Count of Key/value pairs in Dictionary2 = 6
Count of Key/value pairs in Dictionary2 (Updated) = 0

이처럼 Add() 메서드로 요소를 계속 추가할 수 있으며, Clear() 호출 후 Count가 0이 된 것을 통해 모든 요소가 삭제되었음을 확인할 수 있습니다. 참고로 동일한 키가 이미 존재하는 상태에서 Add()를 호출하면 ArgumentException이 발생하므로, 기존 키의 값을 변경하려면 인덱서(dict[key] = value)를 사용하는 것이 안전합니다.