StringDictionary 클래스는 키와 값이 객체(object)가 아닌 문자열(string)로 강력하게 형식화된 해시 테이블(hash table)을 구현하는 컬렉션 클래스입니다. System.Collections.Specialized 네임스페이스에 포함되어 있으며, 키와 값이 모두 반드시 문자열인 경우 일반 해시테이블보다 타입 안정성과 편의성이 뛰어납니다.
StringDictionary 클래스의 주요 속성
| 번호 | 속성 및 설명 |
|---|---|
| 1 | Count StringDictionary에 저장된 키/값 쌍의 개수를 가져옵니다. |
| 2 | IsSynchronized StringDictionary에 대한 액세스가 동기화(스레드로부터 안전)되어 있는지 여부를 나타내는 값을 가져옵니다. |
| 3 | Item[String] 지정한 키와 연결된 값을 가져오거나 설정합니다. |
| 4 | Keys StringDictionary에 있는 키들의 컬렉션을 가져옵니다. |
| 5 | SyncRoot StringDictionary에 대한 액세스를 동기화하는 데 사용할 수 있는 객체를 가져옵니다. |
| 6 | Values StringDictionary에 있는 값들의 컬렉션을 가져옵니다. |
StringDictionary 클래스의 주요 메서드
| 번호 | 메서드 및 설명 |
|---|---|
| 1 | Add(String, String) 지정한 키와 값을 사용하여 항목을 StringDictionary에 추가합니다. |
| 2 | Clear() StringDictionary에서 모든 항목을 제거합니다. |
| 3 | ContainsKey(String) StringDictionary에 특정 키가 포함되어 있는지 확인합니다. |
| 4 | ContainsValue(String) StringDictionary에 특정 값이 포함되어 있는지 확인합니다. |
| 5 | CopyTo(Array, Int32) 문자열 사전의 값들을 지정한 인덱스부터 시작하여 1차원 Array 인스턴스로 복사합니다. |
| 6 | Equals(Object) 지정한 객체가 현재 객체와 같은지 여부를 판단합니다. (Object 클래스에서 상속됨) |
예제 1: 두 StringDictionary 객체의 동등성 비교
두 StringDictionary 객체가 서로 같은지 확인하는 방법은 다음 코드와 같습니다.
using System;
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");
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("A", "John");
strDict2.Add("B", "Andy");
strDict2.Add("C", "Tim");
strDict2.Add("D", "Ryan");
strDict2.Add("E", "Kevin");
strDict2.Add("F", "Katie");
strDict2.Add("G", "Brad");
StringDictionary strDict3 = new StringDictionary();
strDict3 = strDict2;
Console.WriteLine("Is Dictionary2 equal to Dictionary3? = "+strDict2.Equals(strDict3));
Console.WriteLine("Is Dictionary1 equal to Dictionary3? = "+strDict1.Equals(strDict3));
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
Is Dictionary2 equal to Dictionary3? = True Is Dictionary1 equal to Dictionary3? = False
설명: strDict3은 strDict2와 동일한 참조를 가리키므로 두 객체는 같다고 판단되어 True가 출력됩니다. 반면 strDict1은 내용이 같더라도 별도의 객체이므로 참조가 다르기 때문에 False가 반환됩니다.
예제 2: StringDictionary 요소 열거 및 동기화 여부 확인
StringDictionary의 요소를 순회하면서 출력하고, 해당 사전이 동기화(synchronized) 상태인지 확인하는 코드는 다음과 같습니다.
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);
}
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("Is the StringDictionary2 synchronized? = "+strDict2.IsSynchronized);
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E Is the StringDictionary2 synchronized? = False
참고: StringDictionary는 기본적으로 스레드로부터 안전하지 않으므로(IsSynchronized가 False), 멀티스레드 환경에서 안전하게 사용하려면 Synchronized() 메서드로 래핑하거나 별도의 잠금 처리를 해야 합니다. 또한 출력 결과에서 알 수 있듯이 StringDictionary는 키를 소문자로 변환하여 저장한다는 점도 기억해 두면 유용합니다.