首頁  >  文章  >  後端開發  >  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 是否為 synchronized (線程安全)。

5 Item[Object]

取得或設定與指定鍵關聯的值。

6 Keys

取得包含鍵的 ICollection HybridDictionary。

7

SyncRoot8Values以下是HybridDictionary類別的一些方法:
取得可用於同步存取的對象 到HybridDictionary。
取得包含在HybridDictionary中的值的ICollection the HybridDictionary.

#序號

方法與描述

1 #Add(Object, Object)

將具有指定鍵和值的條目新增至 the HybridDictionary.

2 Clear()

從HybridDictionary移除所有項目。

3 Contains(Object)

確定HybridDictionary是否包含特定的鍵。

4 CopyTo(Array, Int32)

#將HybridDictionary的條目複製到一個一維數組中 在指定索引處的陣列實例。

5 Equals(Object) strong>

確定指定的物件是否等於 當前對象。 (Inherited from Object)

6 GetEnumerator()

回傳一個IDictionaryEnumerator,用於遍歷 HybridDictionary。

7

GetHashCode()

用作預設雜湊函數。 (繼承自Object)

8

#GetType()

##8

#GetType()

## 取得目前實例的Type 。 (Inherited from 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("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);
   }
}
###輸出#######這會產生以下輸出−###
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("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);
   }
}
###輸出######這將產生以下輸出−###
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刪除