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

C# SortedDictionary.Count 속성 – 키/값 쌍 개수 확인 방법과 예제

개요

C#에서 SortedDictionary<TKey, TValue> 클래스의 Count 속성은 해당 컬렉션에 현재 저장되어 있는 키/값 쌍(key/value pair)의 개수를 반환합니다. 정렬된 사전 구조에서 요소 수를 빠르게 파악해야 할 때 유용하게 사용됩니다.

문법(Syntax)

Count 속성은 읽기 전용(get 전용) 속성이며, 문법은 다음과 같습니다.

public int Count { get; }

예제 1

다음은 Count 속성의 기본적인 사용 예입니다.

using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(100, "Mobile");
      sortedDict.Add(200, "Laptop");
      sortedDict.Add(300, "Desktop");
      sortedDict.Add(400, "Speakers");
      sortedDict.Add(500, "Headphone");
      sortedDict.Add(600, "Earphone");
      Console.WriteLine("SortedDictionary 키-값 쌍 출력...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("키-값 쌍 개수 = " + sortedDict.Count);
      Console.WriteLine("\n키 200이 존재하는가? = " + sortedDict.ContainsKey(200));
      sortedDict.Clear();
      Console.WriteLine("Clear() 호출 후 키-값 쌍 개수 = " + sortedDict.Count);
   }
}

실행 결과

SortedDictionary 키-값 쌍 출력...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone
키-값 쌍 개수 = 6
키 200이 존재하는가? = True
Clear() 호출 후 키-값 쌍 개수 = 0

위 예제에서는 요소 6개를 추가한 후 Count 값이 6으로 출력되며, Clear() 메서드로 모든 요소를 제거하면 Count 값이 0으로 변경되는 것을 확인할 수 있습니다.

예제 2

이번에는 요소를 추가한 후 Count 값이 어떻게 변하는지 살펴보겠습니다.

using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(100, "Inspiron");
      sortedDict.Add(200, "Alienware");
      sortedDict.Add(300, "Projectors");
      sortedDict.Add(400, "XPS");
      Console.WriteLine("SortedDictionary 키-값 쌍 출력...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("키-값 쌍 개수 = " + sortedDict.Count);
      sortedDict.Add(800, "Notebook");
      sortedDict.Add(10000, "Bluetooth Speaker");
      Console.WriteLine("\nSortedDictionary 키-값 쌍 출력... 업데이트 후");
      demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("업데이트 후 키-값 쌍 개수 = " + sortedDict.Count);
      sortedDict.Clear();
      Console.WriteLine("\nClear() 호출 후 키-값 쌍 개수 = " + sortedDict.Count);
   }
}

실행 결과

SortedDictionary 키-값 쌍 출력...
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
키-값 쌍 개수 = 4
SortedDictionary 키-값 쌍 출력... 업데이트 후
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
Key = 800, Value = Notebook
Key = 10000, Value = Bluetooth Speaker
업데이트 후 키-값 쌍 개수 = 6
Clear() 호출 후 키-값 쌍 개수 = 0

정리

SortedDictionary.Count 속성은 컬렉션에 포함된 키/값 쌍의 개수를 실시간으로 반환합니다. 요소를 추가하면 개수가 증가하고, Clear() 메서드로 모든 항목을 제거하면 0이 됩니다. 또한 SortedDictionary는 키를 기준으로 자동 정렬되므로, 열거 시 항상 정렬된 순서로 출력되는 점도 함께 기억해 두면 좋습니다.