ListDictionary 클래스는 단일 연결 리스트(singly linked list)를 기반으로 IDictionary 인터페이스를 구현한 컬렉션입니다. 일반적으로 항목 수가 10개 미만인 소규모 컬렉션을 처리할 때 사용하도록 권장되며, 적은 데이터에서는 해시 테이블보다 빠른 성능을 보여줍니다.
ListDictionary의 주요 속성
ListDictionary 클래스에서 자주 사용되는 속성은 다음과 같습니다.
| 번호 | 속성 및 설명 |
|---|---|
| 1 | Count ListDictionary에 포함된 키/값 쌍의 개수를 반환합니다. |
| 2 | IsFixedSize ListDictionary의 크기가 고정되어 있는지 여부를 나타내는 값을 반환합니다. |
| 3 | IsReadOnly ListDictionary가 읽기 전용인지 여부를 나타내는 값을 반환합니다. |
| 4 | IsSynchronized ListDictionary가 스레드로부터 안전하게 동기화되어 있는지 여부를 반환합니다. |
| 5 | Item[Object] 지정된 키와 연결된 값을 가져오거나 설정합니다. |
| 6 | Keys ListDictionary의 모든 키를 담고 있는 ICollection을 반환합니다. |
| 7 | SyncRoot ListDictionary에 대한 접근을 동기화하는 데 사용할 수 있는 개체를 반환합니다. |
| 8 | Values ListDictionary의 모든 값을 담고 있는 ICollection을 반환합니다. |
ListDictionary의 주요 메서드
ListDictionary 클래스에서 제공하는 대표적인 메서드는 다음과 같습니다.
| 번호 | 메서드 및 설명 |
|---|---|
| 1 | Add(Object, Object) 지정된 키와 값으로 구성된 항목을 ListDictionary에 추가합니다. |
| 2 | Clear() ListDictionary의 모든 항목을 제거합니다. |
| 3 | Contains(Object) ListDictionary에 특정 키가 포함되어 있는지 확인합니다. |
| 4 | CopyTo(Array, Int32) ListDictionary의 항목들을 지정한 인덱스 위치부터 1차원 배열로 복사합니다. |
| 5 | Equals(Object) 지정된 개체가 현재 개체와 같은지 여부를 판단합니다. (Object 클래스에서 상속) |
| 6 | GetEnumerator() ListDictionary 전체를 반복할 수 있는 IDictionaryEnumerator를 반환합니다. |
| 7 | GetHashCode() 기본 해시 함수 역할을 수행합니다. (Object 클래스에서 상속) |
| 8 | GetType() 현재 인스턴스의 Type을 반환합니다. (Object 클래스에서 상속) |
예제 1: 읽기 전용 및 고정 크기 확인하기
다음 예제는 ListDictionary가 읽기 전용인지, 고정 크기인지, 동기화되어 있는지 확인하는 방법을 보여줍니다.
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 elements...");
foreach(DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Is the ListDictionary1 having fixed size? = "+dict1.IsFixedSize);
Console.WriteLine("If ListDictionary1 read-only? = "+dict1.IsReadOnly);
Console.WriteLine("Is ListDictionary1 synchronized = "+dict1.IsSynchronized);
Console.WriteLine("The ListDictionary1 has the key M? = "+dict1.Contains("M"));
ListDictionary dict2 = new ListDictionary();
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("\nListDictionary2 key-value pairs...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = "+ demoEnum.Value);
Console.WriteLine("Is the ListDictionary2 having fixed size? = "+dict2.IsFixedSize);
Console.WriteLine("If ListDictionary2 read-only? = "+dict2.IsReadOnly);
Console.WriteLine("Is ListDictionary2 synchronized = "+dict2.IsSynchronized);
Console.WriteLine("The ListDictionary2 has the key 5? = "+dict2.Contains("5"));
}
}
실행 결과
ListDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Is the ListDictionary1 having fixed size? = False If ListDictionary1 read-only? = False Is ListDictionary1 synchronized = False The ListDictionary1 has the key M? = False ListDictionary2 key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Is the ListDictionary2 having fixed size? = False If ListDictionary2 read-only? = False Is ListDictionary2 synchronized = False The ListDictionary2 has the key 5? = True
위 실행 결과를 보면 ListDictionary는 기본적으로 고정 크기가 아니며(IsFixedSize = False), 읽기 전용도 아니고(IsReadOnly = False), 별도의 동기화 처리도 되어 있지 않습니다(IsSynchronized = False). 또한 Contains() 메서드를 통해 특정 키의 존재 여부를 간편하게 확인할 수 있습니다.
예제 2: 두 ListDictionary 개체 비교하기
다음 예제는 두 ListDictionary 개체가 같은지 Equals() 메서드로 비교하는 방법을 보여줍니다.
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 elements...");
foreach(DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict2 = new ListDictionary();
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("\nListDictionary2 elements...");
foreach(DictionaryEntry d in dict2) {
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict3 = new ListDictionary();
dict3 = dict2;
Console.WriteLine("\nIs ListDictionary3 equal to ListDictionary2? = "+(dict3.Equals(dict2)));
}
}
실행 결과
ListDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear ListDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Is ListDictionary3 equal to ListDictionary2? = True
dict3에 dict2를 할당하면 두 변수는 같은 개체를 참조하게 되므로, Equals() 메서드의 결과가 True로 출력됩니다. 이처럼 참조 타입 비교 시에는 내용이 아닌 개체의 참조가 같은지를 기준으로 판별됩니다.