C#에서 Hashtable이 읽기 전용(Read-Only) 상태인지 확인하려면 IsReadOnly 속성을 사용하면 됩니다. 이 속성은 해당 컬렉션이 수정 가능한지 여부를 나타내는 불리언(Boolean) 값을 반환합니다. 아래 예제를 통해 자세히 살펴보겠습니다.
예제 1
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash = new Hashtable();
hash.Add("One", "Katie");
hash.Add("Two", "John");
hash.Add("Three", "Barry");
hash.Add("Four", "");
hash.Add("Five","Harry");
hash.Add("Six", "F");
hash.Add("Seven", "Tom");
hash.Add("Eight","Andy");
hash.Add("Nine", "I");
hash.Add("Ten", "Tim");
Console.WriteLine("Hashtable Key and Value pairs...");
foreach(DictionaryEntry entry in hash){
Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
}
Console.WriteLine("Is the Hashtable having fixed size? = "+hash.IsFixedSize);
Console.WriteLine("If Hashtable read-only? = "+hash.IsReadOnly);
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
Hashtable Key and Value pairs... One and Katie Ten and Tim Five and Harry Three and Barry Seven and Tom Two and John Four and Eight and Andy Nine and I Six and F Is the Hashtable having fixed size? = False If Hashtable read-only? = False
출력 결과에서 볼 수 있듯이, 일반적인 방법으로 생성한 Hashtable의 IsFixedSize와 IsReadOnly 속성은 모두 False입니다. 즉, 크기가 고정되어 있지 않고 요소를 자유롭게 추가·수정·삭제할 수 있습니다.
예제 2
이번에는 숫자 키를 사용하는 또 다른 예제를 살펴보겠습니다.
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash = new Hashtable();
hash.Add("1", "A");
hash.Add("2", "B");
hash.Add("3", "C");
hash.Add("4", "D");
hash.Add("5","E");
hash.Add("6", "F");
hash.Add("7", "G");
hash.Add("8","H");
hash.Add("9", "I");
hash.Add("10", "J");
Console.WriteLine("Hashtable Key and Value pairs...");
foreach(DictionaryEntry entry in hash){
Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
}
Console.WriteLine("Is Hashtable having fixed size? = "+hash.IsFixedSize);
Console.WriteLine("If Hashtable read-only? = "+hash.IsReadOnly);
}
}출력 결과
실행 결과는 다음과 같습니다.
Hashtable Key and Value pairs... 10 and J 1 and A 2 and B 3 and C 4 and D 5 and E 6 and F 7 and G 8 and H 9 and I Is Hashtable having fixed size? = False If Hashtable read-only? = False
참고 사항
IsReadOnly속성: Hashtable이 읽기 전용이면 true, 아니면 false를 반환합니다.IsFixedSize속성: Hashtable의 크기가 고정되어 있으면 true, 아니면 false를 반환합니다.- 일반적으로
new Hashtable()로 생성한 인스턴스는 읽기 전용이 아니므로 두 속성 모두 false가 출력됩니다.