📌  相关文章
📜  C#|获取或设置与StringDictionary中的指定键关联的值

📅  最后修改于: 2021-05-30 00:00:34             🧑  作者: Mango

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