C# | Get or set the value associated with the specified key in StringDictionary
StringDictionary is a specialized collection. It is found in the System.Collections.Specialized namespace. It only allows string keys and string values. It suffers from performance problems. It implements a hash table with the key and the value strongly typed to be strings rather than objects.
Below given are some examples to understand the implementation in a better way :
Example 1:
// C# code to get or set the value// associated with the specified key// in StringDictionaryusing 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); } }} |
Output:
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


