>  기사  >  백엔드 개발  >  C#의 혼합 사전 클래스?

C#의 혼합 사전 클래스?

王林
王林앞으로
2023-09-08 11:57:09679검색

C# 中的混合字典类?

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

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

일련 번호 속성 및 설명
1 Count

포함된 키/값 쌍 수 가져오기

혼합 사전.

2 IsFixedSize

여부를 나타내는 값을 가져옵니다. HybridDictionary의 크기는 고정되어 있습니다.

3 IsReadOnly

HybridDictionary가 다음과 같은지 여부를 나타내는 값을 가져옵니다. 읽기 전용입니다.

4 IsSynchronized

HybridDictionary가 다음과 같은지 여부를 나타내는 값을 가져옵니다. 동기화됨(스레드 안전).

5 Item[Object]

지정된 키와 연결된 값을 가져오거나 설정합니다.

6 Keys

키가 포함된 ICollection 가져오기 하이브리드사전.

7 SyncRoot

동기 액세스에 사용할 수 있는 개체 가져오기 HybridDictionary에.

8 Values

HybridDictionary에 포함된 값의 ICollection 가져오기 HybridDictionary.

다음은 HybridDictionary 클래스의 몇 가지 메서드입니다.

일련 번호 메서드 및 설명
1 추가 (객체, 객체)

지정된 키와 값이 있는 항목이 추가됩니다. HybridDictionary.

2 Clear()

HybridDictionary에서 모든 항목을 제거합니다.

3 Contains(Object)

HybridDictionary에 특정 키가 포함되어 있는지 확인합니다.

4 CopyTo(Array, Int32)

HybridDictionary의 항목을 1차원 배열로 복사합니다. 지정된 인덱스에 있는 배열 인스턴스입니다.

5 Equals(Object)

지정된 객체가 다음과 같은지 확인 현재 객체. (Object에서 상속됨)

6 GetEnumerator()

순회를 위한 IDictionaryEnumerator를 반환합니다. 하이브리드사전.

7 GetHashCode() strong>

가 기본 해시 함수로 사용됩니다. (Object에서 상속됨)

8 GetType()

현재 인스턴스의 유형을 가져옵니다. (Inherited from Object)

HybridDictionary의 키-값 쌍 수를 계산하는 코드는 다음과 같습니다. −

Example

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

Demonstration

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("HybridDictionary2 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);
   }
}

Output

그러면 다음과 같은 출력이 생성됩니다. −

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가 동기화되었는지 확인하기 위해 코드는 다음과 같습니다. −

Example

Live Demonstration

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("HybridDictionary2 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);
   }
}

Output

이렇게 하면 다음과 같은 출력이 생성됩니다−

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

위 내용은 C#의 혼합 사전 클래스?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제