StringDictionary는 System.Collections.Specialized 네임스페이스에 포함된 특수 컬렉션으로, 키와 값이 모두 문자열로만 구성된 해시 테이블입니다. 이 컬렉션에 저장된 키/값 쌍의 총 개수를 확인하려면 Count 속성을 사용하면 됩니다.
Count 속성은 컬렉션에 현재 포함된 요소의 개수를 int 형식으로 반환하며, 요소가 추가되거나 제거될 때마다 값이 자동으로 갱신됩니다.
예제 1: Count 속성으로 키/값 쌍 개수 확인하기
StringDictionary에 네 개의 요소를 추가한 후, GetEnumerator() 메서드로 각 요소를 순회하여 출력하고 마지막에 Count 속성으로 전체 개수를 확인하는 기본 예제입니다.
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringDictionary strDict = new StringDictionary();
strDict.Add("1", "One");
strDict.Add("2", "Two");
strDict.Add("3", "Three");
strDict.Add("4", "Four");
Console.WriteLine("StringDictionary key-value pairs...");
IEnumerator demoEnum = strDict.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of StringDictionary key-value pairs..."+strDict.Count);
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
StringDictionary key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Count of StringDictionary key-value pairs...4
실행 결과를 보면 Add() 메서드로 추가한 네 개의 키/값 쌍이 순서대로 출력되고, Count 속성이 전체 개수인 4를 반환하는 것을 확인할 수 있습니다.
예제 2: 두 개의 StringDictionary 다루기
이번에는 서로 다른 크기의 두 StringDictionary를 생성하고, 하나는 foreach 문으로, 다른 하나는 IEnumerator로 요소를 순회한 뒤 각각의 개수를 비교해 보겠습니다.
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 elements...");
foreach(DictionaryEntry de in strDict1){
Console.WriteLine(de.Key + " " + de.Value);
}
Console.WriteLine("Count of StringDictionary1 key-value pairs..."+strDict1.Count);
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("
StringDictionary2 key-value pairs...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of StringDictionary2 key-value pairs..."+strDict2.Count);
}
}출력 결과
위 프로그램의 실행 결과는 다음과 같습니다.
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad Count of StringDictionary1 key-value pairs...7 StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E Count of StringDictionary2 key-value pairs...5
여기서 주목할 점은 StringDictionary1의 키가 소문자(a, b, c...)로 출력된다는 것입니다. StringDictionary는 키의 대소문자를 구분하지 않기 때문에, 키를 추가하면 내부적으로 모두 소문자로 변환되어 저장됩니다. 반면 값(Value)은 입력된 원본 문자열 그대로 유지됩니다.
핵심 정리
- StringDictionary의 요소 개수는 Count 속성으로 확인하며, 반환 형식은 int입니다.
- 요소 추가는 Add() 메서드로, 순회는 GetEnumerator() 또는 foreach 문으로 처리합니다.
- 키는 대소문자를 구분하지 않아 내부적으로 소문자로 변환되며, 값은 원본 그대로 저장됩니다.
- StringDictionary를 사용하려면 System.Collections.Specialized 네임스페이스를 참조해야 합니다.