StringDictionary是一個專門的集合。此類位於 System.Collections.Specialized 命名空間下。它隻允許字符串鍵和字符串值。它存在性能問題。它實現了一個哈希表,其中鍵和值強類型化為字符串而不是對象。
特征:
- 鑰匙不能空值,但一個值可以。
- 鍵以不區分大小寫的方式處理,即在與字符串字典一起使用之前將其轉換為小寫。
- 構造函數StringDictionary()初始化 StringDictionary 類的新實例。
Constructors
構造函數 | 說明 |
---|---|
StringDictionary() | 初始化 StringDictionary 類的新實例。 |
例子:
// C# code to create a StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a StringDictionary named myDict
StringDictionary myDict = new StringDictionary();
// Adding key and value into the StringDictionary
myDict.Add("A", "Apple");
myDict.Add("B", "Banana");
myDict.Add("C", "Cat");
myDict.Add("D", "Dog");
myDict.Add("E", "Elephant");
// Displaying the keys and values in StringDictionary
foreach(DictionaryEntry dic in myDict)
{
Console.WriteLine(dic.Key + " " + dic.Value);
}
}
}
輸出:
d Dog b Banana c Cat e Elephant a Apple
Properties
屬性 | 說明 |
---|---|
Count | 獲取 StringDictionary 中鍵/值對的數量。 |
IsSynchronized | 獲取一個值,該值指示對 StringDictionary 的訪問是否同步(線程安全)。 |
項目[字符串] | 獲取或設置與指定鍵關聯的值。 |
Keys | 獲取 StringDictionary 中的鍵的集合。 |
SyncRoot | 獲取可用於同步對 StringDictionary 的訪問的對象。 |
Values | 獲取 StringDictionary 中的值的集合。 |
例子:
// C# code illustrate the Properties of
// StringDictionary class
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a StringDictionary named myDict
StringDictionary myDict = new StringDictionary();
// Adding key and value into the StringDictionary
myDict.Add("3", "prime & odd");
myDict.Add("2", "prime & even");
myDict.Add("4", "non-prime & even");
myDict.Add("9", "non-prime & odd");
// -------- Values Property --------
// Getting a collection of values
// in the StringDictionary
foreach(string val in myDict.Values)
{
Console.WriteLine(val);
}
// -------- IsSynchronized Property --------
// Checking if StringDictionary
// is synchronized (thread safe)
Console.WriteLine(myDict.IsSynchronized);
}
}
輸出:
prime & even prime & odd non-prime & odd non-prime & even False
Methods
方法 | 說明 |
---|---|
Add(String, String) | 將具有指定鍵和值的條目添加到 StringDictionary 中。 |
Clear() | 從 StringDictionary 中刪除所有條目。 |
ContainsKey(String) | 確定 StringDictionary 是否包含特定 key 。 |
ContainsValue(String) | 確定 StringDictionary 是否包含特定值。 |
複製到(數組,Int32) | 將字符串字典值複製到指定索引處的一維 Array 實例。 |
Equals(Object) | 確定指定對象是否等於當前對象。 |
GetEnumerator() | 返回一個遍曆字符串字典的枚舉器。 |
GetHashCode() | 用作默認的哈希函數。 |
GetType() | 獲取當前實例的類型。 |
MemberwiseClone() | 創建當前對象的淺拷貝。 |
Remove(String) | 從字符串字典中刪除具有指定鍵的條目。 |
ToString() | 返回表示當前對象的字符串。 |
示例 1:
// C# code to remove the entry
// with the specified key from
// the StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a StringDictionary named myDict
StringDictionary myDict = new StringDictionary();
// Adding key and value into the StringDictionary
myDict.Add("A", "Apple");
myDict.Add("B", "Banana");
myDict.Add("C", "Cat");
myDict.Add("D", "Dog");
myDict.Add("E", "Elephant");
myDict.Add("F", "Fish");
// Displaying the keys and values in StringDictionary
Console.WriteLine("The number of key/value pairs are : " + myDict.Count);
// Removing the entry with the specified
// key from the StringDictionary
myDict.Remove("D");
// Displaying the keys and values in StringDictionary
Console.WriteLine("The number of key/value pairs are : " + myDict.Count);
}
}
輸出:
The number of key/value pairs are : 6 The number of key/value pairs are : 5
示例 2:
// C# code to copy StringDictionary
// to Array at the specified index
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a StringDictionary named myDict
StringDictionary myDict = new StringDictionary();
// Adding key and value into the StringDictionary
myDict.Add("A", "Apple");
myDict.Add("B", "Banana");
myDict.Add("C", "Cat");
myDict.Add("D", "Dog");
myDict.Add("E", "Elephant");
// Creating an Array named myArr
DictionaryEntry[] myArr = { new DictionaryEntry(),
new DictionaryEntry(),
new DictionaryEntry(),
new DictionaryEntry(),
new DictionaryEntry() };
// Copying StringDictionary to
// Array at the specified index
myDict.CopyTo(myArr, 0);
// Displaying key and value pairs in Array myArr
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine(myArr[i].Key + " " + myArr[i].Value);
}
}
}
輸出:
d Dog b Banana c Cat e Elephant a Apple
參考:
相關用法
- C# String Clone()用法及代碼示例
- C# String Compare()用法及代碼示例
- C# String CompareOrdinal()用法及代碼示例
- C# String CompareTo()用法及代碼示例
- C# String Concat()用法及代碼示例
- C# String Contains()用法及代碼示例
- C# String Copy()用法及代碼示例
- C# String CopyTo()用法及代碼示例
- C# String EndsWith()用法及代碼示例
- C# String Equals()用法及代碼示例
- C# String Format()用法及代碼示例
- C# String GetEnumerator()用法及代碼示例
- C# String IndexOf()用法及代碼示例
- C# String Insert()用法及代碼示例
- C# String IsInterned()用法及代碼示例
- C# String IsNormalized()用法及代碼示例
- C# String IsNullOrEmpty()用法及代碼示例
- C# String IsNullOrWhiteSpace()用法及代碼示例
- C# String Join()用法及代碼示例
- C# String LastIndexOf()用法及代碼示例
- C# String LastIndexOfAny()用法及代碼示例
- C# String Normalize()用法及代碼示例
- C# String PadLeft()用法及代碼示例
- C# String PadRight()用法及代碼示例
- C# String Remove()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 C# | StringDictionary Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。