Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#의 HybridDictionary 클래스?

<시간/>

HybridDictionary 클래스는 컬렉션이 작을 때 ListDictionary를 사용하고 컬렉션이 커지면 Hashtable로 전환하여 IDictionary를 구현합니다.

다음은 HybridDictionary 클래스의 속성입니다 -

Sr.No 속성 및 설명
1 카운트
HybridDictionary에 포함된 키/값 쌍의 수를 가져옵니다.
2 IsFixedSize
HybridDictionary의 크기가 고정되어 있는지 여부를 나타내는 값을 가져옵니다.
3 읽기 전용
HybridDictionary가 읽기 전용인지 여부를 나타내는 값을 가져옵니다.
4 동기화됨
HybridDictionary가 동기화되었는지(스레드 안전) 여부를 나타내는 값을 가져옵니다.
5 항목[객체]
지정된 키와 연결된 값을 가져오거나 설정합니다.
6
HybridDictionary의 키를 포함하는 ICollection을 가져옵니다.
7 SyncRoot
HybridDictionary에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다.
8
HybridDictionary의 값을 포함하는 ICollection을 가져옵니다.

다음은 HybridDictionary 클래스의 메소드 중 일부입니다 -

Sr.No 방법 및 설명
1 추가(객체, 객체)
지정된 키와 값이 있는 항목을 HybridDictionary에 추가합니다.
2 지우기()
HybridDictionary에서 모든 항목을 제거합니다.
3 포함(객체)
HybridDictionary에 특정 키가 포함되어 있는지 확인합니다.p>
4 CopyTo(배열, Int32)
지정된 인덱스의 onedimensionalArray 인스턴스에 HybridDictionary 항목을 복사합니다.
5 같음(객체)
지정된 개체가 현재 개체와 같은지 여부를 확인합니다. (Object에서 상속됨)
6 GetEnumerator()
HybridDictionary를 반복하는 IDictionaryEnumerator를 반환합니다.
7 GetHashCode()
기본 해시 함수 역할을 합니다.(Object에서 상속됨)
8 GetType()
현재 인스턴스의 Type을 가져옵니다. (Object에서 상속됨)

HybridDictionary에서 키/값 쌍의 수를 계산하기 위한 코드는 다음과 같습니다. -

이제 몇 가지 예를 살펴보겠습니다 -

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      HybridDictionary dict1 = new HybridDictionary();
      dict1.Add("A", "SUV");
      dict1.Add("B", "MUV");
      dict1.Add("C", "AUV");
      Console.WriteLine("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of Key/value pairs in Dictionary1 = "+dict1.Count);
      HybridDictionary dict2 = new HybridDictionary();
      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("\nHybridDictionary2 elements...");
      foreach(DictionaryEntry d in dict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of Key/value pairs in Dictionary2 = "+dict1.Count);
      dict2.Clear();
      Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = "+dict2.Count);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

HybridDictionary1 elements...
A SUV
B MUV
C AUV
Count of Key/value pairs in Dictionary1 = 3
HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Count of Key/value pairs in Dictionary2 = 3
Count of Key/value pairs in Dictionary2 (Updated) = 0

HybridDictionary가 동기화되었는지 확인하기 위한 코드는 다음과 같습니다. -

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      HybridDictionary dict1 = new HybridDictionary();
      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("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Is the HybridDictionary1 having fixed size? = "+dict1.IsFixedSize);
      Console.WriteLine("If HybridDictionary1 read-only? = "+dict1.IsReadOnly);
      Console.WriteLine("Is HybridDictionary1 synchronized = "+dict1.IsSynchronized);
      HybridDictionary dict2 = new HybridDictionary();
      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("\nHybridDictionary2 elements...");
      foreach(DictionaryEntry d in dict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Is HybridDictionary1 equal to HybridDictionary2? = "+(dict1.Equals(dict2)));
      Console.WriteLine("Is the HybridDictionary2 having fixed size? = "+dict2.IsFixedSize);
      Console.WriteLine("If HybridDictionary2 read-only? = "+dict2.IsReadOnly);
      Console.WriteLine("Is HybridDictionary2 synchronized = "+dict2.IsSynchronized);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

HybridDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Is the HybridDictionary1 having fixed size? = False
If HybridDictionary1 read-only? = False
Is HybridDictionary1 synchronized = False
HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Is HybridDictionary1 equal to HybridDictionary2? = False
Is the HybridDictionary2 having fixed size? = False
If HybridDictionary2 read-only? = False
Is HybridDictionary2 synchronized = False