StringDictionary是一个专门的集合。在System.Collections.Specialized命名空间中找到它。它仅允许字符串键和字符串值。它遭受性能问题的困扰。它使用键和强类型化为字符串而不是对象的值实现哈希表。
下面给出了一些示例,以更好地理解实现:
范例1:
// C# code to get or set the value
// associated with the specified key
// in 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("G", "GeeksforGeeks");
myDict.Add("C", "Coding");
myDict.Add("DS", "Data Structures");
myDict.Add("N", "Noida");
myDict.Add("GC", "Geeks Classes");
// Displaying the keys and values
// in StringDictionary
foreach(DictionaryEntry de in myDict)
{
Console.WriteLine(de.Key + " " + de.Value);
}
// To get the value corresponding to key "C"
Console.WriteLine(myDict["C"]);
// Changing the value corresponding to key "C"
myDict["C"] = "C++";
// To get the value corresponding to key "C"
Console.WriteLine(myDict["C"]);
// To get the value corresponding to key "N"
Console.WriteLine(myDict["N"]);
// Changing the value corresponding to key "N"
myDict["N"] = "North";
// To get the value corresponding to key "N"
Console.WriteLine(myDict["N"]);
// Displaying the keys and values
// in StringDictionary
foreach(DictionaryEntry de in myDict)
{
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
输出:
gc Geeks Classes
c Coding
n Noida
ds Data Structures
g GeeksforGeeks
Coding
C++
Noida
North
c C++
g GeeksforGeeks
gc Geeks Classes
ds Data Structures
n North