StringDictionary是一个专门的集合。此类位于System.Collections.Specialized命名空间下。它仅允许字符串键和字符串值。它遭受性能问题的困扰。它使用键和强类型化为字符串而不是对象的值实现哈希表。
特征:
- 键不能为null ,但值可以。
- 密钥以不区分大小写的方式处理,即在与字符串字典一起使用之前将其转换为小写形式。
- 构造函数StringDictionary()初始化StringDictionary类的新实例。
建设者
Constructor | Description |
---|---|
StringDictionary() | Initializes a new instance of the StringDictionary class. |
例子:
// 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
特性
Property | Description |
---|---|
Count | Gets the number of key/value pairs in the StringDictionary. |
IsSynchronized | Gets a value indicating whether access to the StringDictionary is synchronized (thread safe). |
Item[String] | Gets or sets the value associated with the specified key. |
Keys | Gets a collection of keys in the StringDictionary. |
SyncRoot | Gets an object that can be used to synchronize access to the StringDictionary. |
Values | Gets a collection of values in the 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
方法
Method | Description |
---|---|
Add(String, String) | Adds an entry with the specified key and value into the StringDictionary. |
Clear() | Removes all entries from the StringDictionary. |
ContainsKey(String) | Determines if the StringDictionary contains a specific key. |
ContainsValue(String) | Determines if the StringDictionary contains a specific value. |
CopyTo(Array, Int32) | Copies the string dictionary values to a one-dimensional Array instance at the specified index. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
GetEnumerator() | Returns an enumerator that iterates through the string dictionary. |
GetHashCode() | Serves as the default hash function. |
GetType() | Gets the Type of the current instance. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
Remove(String) | Removes the entry with the specified key from the string dictionary. |
ToString() | Returns a string that represents the current object. |
范例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
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.stringdictionary?view=netframework-4.7.2