C# 中的 StringDictionary 類?


StringDictionay 類實現了一個雜湊表,其鍵和值都強型別化為字串,而不是物件。

以下是 StringDictionary 類的屬性:

序號屬性及描述
1Count
獲取 StringDictionary 中鍵值對的數量。
2IsSynchronized
獲取一個值,該值指示對 StringDictionary 的訪問是否同步(執行緒安全)。
3tem[String]
獲取或設定與指定鍵關聯的值。
4Keys
獲取 StringDictionary 中鍵的集合。
5SyncRoot
獲取一個物件,該物件可用於同步對 StringDictionary 的訪問。
6Values
獲取 StringDictionary 中值的集合。

以下是 StringDictionary 類的一些方法:

序號方法及描述
1Add(String, String)
將具有指定鍵和值的條目新增到 StringDictionary 中。
2Clear()
從 StringDictionary 中移除所有條目。
3ContainsKey(String)
確定 StringDictionary 是否包含特定的鍵。
4ContainsValue(String)
確定 StringDictionary 是否包含特定的值。
5CopyTo(Array, Int32)
將字串字典值複製到指定索引處的一維陣列例項中。
6Equals(Object)
確定指定物件是否等於當前物件。(從 Object 繼承)

示例

現在讓我們看一些示例:

要檢查兩個 StringDictionary 物件是否相等,程式碼如下:

即時演示

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("A", "John");
      strDict2.Add("B", "Andy");
      strDict2.Add("C", "Tim");
      strDict2.Add("D", "Ryan");
      strDict2.Add("E", "Kevin");
      strDict2.Add("F", "Katie");
      strDict2.Add("G", "Brad");
      StringDictionary strDict3 = new StringDictionary();
      strDict3 = strDict2;
      Console.WriteLine("Is Dictionary2 equal to Dictionary3? = "+strDict2.Equals(strDict3));
      Console.WriteLine("Is Dictionary1 equal to Dictionary3? = "+strDict1.Equals(strDict3));
   }
}

輸出

這將產生以下輸出:

Is Dictionary2 equal to Dictionary3? = True
Is Dictionary1 equal to Dictionary3? = False

示例

要檢查 StringDictionary 是否同步,程式碼如下:

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      Console.WriteLine("StringDictionary1 elements...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("1", "A");
      strDict2.Add("2", "B");
      strDict2.Add("3", "C");
      strDict2.Add("4", "D");
      strDict2.Add("5", "E");
      Console.WriteLine("
StringDictionary2 key-value pairs...");       IEnumerator demoEnum = strDict2.GetEnumerator();       DictionaryEntry d;       while (demoEnum.MoveNext()) {          d = (DictionaryEntry)demoEnum.Current;          Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);       }       Console.WriteLine("Is the StringDictionary2 synchronized? = "+strDict2.IsSynchronized);    } }

輸出

這將產生以下輸出:

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary2 key-value pairs...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E
Is the StringDictionary2 synchronized? = False

更新於: 2019-12-16

148 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.