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

C#에서 SortedList의 용량(Capacity) 확인 방법

C# SortedList의 용량이란?

C#에서 SortedList는 키를 기준으로 자동 정렬되는 키-값 쌍 컬렉션입니다. SortedList에 담긴 실제 요소의 개수는 Count 속성으로 확인할 수 있고, 내부적으로 확보된 저장 공간의 크기는 Capacity 속성으로 확인할 수 있습니다.

SortedList는 요소가 추가될 때마다 필요에 따라 용량을 자동으로 늘리는데, 기본 생성자로 생성한 경우 초기 용량은 0이며 첫 번째 요소가 추가될 때 16으로 설정됩니다.

예제 1: SortedList의 Count와 Capacity 출력하기

다음은 SortedList의 용량을 가져오는 예제 코드입니다.

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList sortedList = new SortedList();
      sortedList.Add("A", "1");
      sortedList.Add("B", "2");
      sortedList.Add("C", "3");
      sortedList.Add("D", "4");
      sortedList.Add("E", "5");
      sortedList.Add("F", "6");
      sortedList.Add("G", "7");
      sortedList.Add("H", "8");
      sortedList.Add("I", "9");
      sortedList.Add("J", "10");

      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in sortedList) {
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }

      Console.WriteLine("Count of SortedList key-value pairs = " + sortedList.Count);
      Console.WriteLine("Capacity of SortedList = " + sortedList.Capacity);

      Console.WriteLine("\nEnumerator to iterate through the SortedList...");
      IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
   }
}

출력 결과

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

SortedList elements...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
Key = D, Value = 4
Key = E, Value = 5
Key = F, Value = 6
Key = G, Value = 7
Key = H, Value = 8
Key = I, Value = 9
Key = J, Value = 10
Count of SortedList key-value pairs = 10
Capacity of SortedList = 16

Enumerator to iterate through the SortedList...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
Key = D, Value = 4
Key = E, Value = 5
Key = F, Value = 6
Key = G, Value = 7
Key = H, Value = 8
Key = I, Value = 9
Key = J, Value = 10

위 실행 결과에서 알 수 있듯이, SortedList에는 10개의 키-값 쌍이 들어 있지만(Count = 10) 실제 용량은 16입니다. 이는 SortedList가 향후 요소 추가를 대비해 미리 여유 공간을 확보하기 때문입니다.

예제 2: Clear() 호출 후에도 유지되는 용량

이번에는 Clear() 메서드를 사용해 SortedList의 모든 요소를 제거한 후, Count와 Capacity가 어떻게 변하는지 살펴보겠습니다.

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList sortedList = new SortedList();
      sortedList.Add("A", "1");
      sortedList.Add("B", "2");
      sortedList.Add("C", "3");
      sortedList.Add("D", "4");
      sortedList.Add("E", "5");
      sortedList.Add("F", "6");

      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in sortedList) {
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }

      Console.WriteLine("Count of SortedList key-value pairs = " + sortedList.Count);
      Console.WriteLine("Capacity of SortedList = " + sortedList.Capacity);

      sortedList.Clear();

      Console.WriteLine("Count of SortedList key-value pairs = " + sortedList.Count);
      Console.WriteLine("Capacity of SortedList = " + sortedList.Capacity);
   }
}

출력 결과

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

SortedList elements...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
Key = D, Value = 4
Key = E, Value = 5
Key = F, Value = 6
Count of SortedList key-value pairs = 6
Capacity of SortedList = 16
Count of SortedList key-value pairs = 0
Capacity of SortedList = 16

정리

Clear() 메서드를 호출하면 SortedList의 모든 요소가 제거되어 Count가 0이 됩니다. 하지만 Capacity는 그대로 16으로 유지되는 것을 확인할 수 있습니다. 즉, Clear()는 내부 저장 공간을 해제하지 않고 요소만 비우기 때문에, 동일한 SortedList 객체에 다시 데이터를 추가할 때 성능상 이점을 얻을 수 있습니다. 만약 용량까지 줄이고 싶다면 TrimToSize() 메서드를 사용하면 됩니다.