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

C# OrderedDictionary의 키/값 쌍 개수 구하는 방법

OrderedDictionary에 포함된 키/값 쌍의 개수를 확인하려면 Count 속성을 사용하면 됩니다. Count 속성은 컬렉션 내부에 실제로 저장된 요소의 총 개수를 정수(int) 형태로 반환하며, 요소를 추가하거나 제거할 때마다 자동으로 갱신됩니다.

C# 코드 예제

다음은 OrderedDictionary를 생성하고 Count 속성으로 요소 개수를 출력한 뒤, Clear() 메서드로 모든 요소를 삭제했을 때 개수가 어떻게 변하는지 보여주는 예제입니다.

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main(){
      OrderedDictionary dict = new OrderedDictionary();
      dict.Add("A", "Home Appliances");
      dict.Add("B", "Electronics");
      dict.Add("C", "Smart Wearables");
      dict.Add("D", "Pet Supplies");
      dict.Add("E", "Clothing");
      dict.Add("F", "Footwear");

      Console.WriteLine("OrderedDictionary elements...");
      foreach(DictionaryEntry d in dict){
         Console.WriteLine(d.Key + " " + d.Value);
      }

      Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);

      dict.Clear();
      Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
   }
}

실행 결과

OrderedDictionary elements...
A Home Appliances
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of elements in OrderedDictionary = 6
Count of elements in OrderedDictionary (Updated)= 0

위 결과에서 볼 수 있듯이, 처음에는 6개의 키/값 쌍이 저장되어 있어 Count 값이 6으로 출력됩니다. 이후 Clear() 메서드를 호출해 모든 요소를 제거하면 Count 값이 0으로 갱신되는 것을 확인할 수 있습니다.

추가 예제: 두 개의 OrderedDictionary 비교하기

이번에는 두 개의 OrderedDictionary를 만들어 각각의 Count 값을 확인하고, Equals() 메서드로 두 사전을 비교하며, AsReadOnly() 메서드로 읽기 전용 사전을 생성하는 예제를 살펴보겠습니다.

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main(){
      OrderedDictionary dict1 = new OrderedDictionary();
      dict1.Add("A", "John");
      dict1.Add("B", "Andy");
      dict1.Add("C", "Jacob");
      dict1.Add("D", "Kevin");
      dict1.Add("E", "Gary");
      dict1.Add("F", "Nathan");
      dict1.Add("G", "Kevin");
      dict1.Add("H", "Ryan");

      Console.WriteLine("OrderedDictionary1 elements...");
      foreach(DictionaryEntry d in dict1){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of key-value pairs in OrderedDictionary1 = " + dict1.Count);

      OrderedDictionary dict2 = new OrderedDictionary();
      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("\nOrderedDictionary2 elements...");
      foreach(DictionaryEntry d in dict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of key-value pairs in OrderedDictionary2 = " + dict2.Count);

      Console.WriteLine("\nIs OrderedDictionary1 equal to OrderedDictionary2? = "+(dict1.Equals(dict2)));

      OrderedDictionary orderedDict = dict2.AsReadOnly();
      Console.WriteLine("Is OrderedDictionary read-only? = "+orderedDict.IsReadOnly);
   }
}

실행 결과

OrderedDictionary1 elements...
A John
B Andy
C Jacob
D Kevin
E Gary
F Nathan
G Kevin
H Ryan
Count of key-value pairs in OrderedDictionary1 = 8 

OrderedDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Count of key-value pairs in OrderedDictionary2 = 6

Is OrderedDictionary1 equal to OrderedDictionary2? = False
Is OrderedDictionary read-only? = True

결과 분석

  • dict1에는 8개의 키/값 쌍이, dict2에는 6개의 키/값 쌍이 저장되어 있어 Count 값이 각각 8과 6으로 출력됩니다.
  • 두 사전은 저장된 키와 값이 서로 다르므로 Equals() 메서드의 결과는 False입니다.
  • AsReadOnly() 메서드로 생성된 사전은 수정할 수 없는 읽기 전용 컬렉션이므로 IsReadOnly 속성이 True를 반환합니다.

이처럼 C#의 OrderedDictionary 클래스는 System.Collections.Specialized 네임스페이스에 속하며, 삽입 순서를 유지하는 키/값 컬렉션이 필요할 때 유용하게 사용할 수 있습니다.