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 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); } } } |
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
Recommended Posts:
- C# | Add key and value into StringDictionary
- How to create a StringDictionary in C#
- C# | StringDictionary Class
- C# | Gets or sets the value at the specified key in StringDictionary
- C# | Get a collection of values in the StringDictionary
- How to get Synchronize access to the StringDictionary in C#
- C# | Get a collection of keys in the StringDictionary
- C# | Check if the StringDictionary contains a specific key
- C# | Remove entry with specified key from the StringDictionary
- C# | Removing all entries from the StringDictionary
- C# | Check if the StringDictionary contains a specific value
- C# | Get an enumerator that iterates through the stringDictionary
- C# | Get the number of key/value pairs in the StringDictionary
- C# | Copy StringDictionary to Array at the specified index
- C# | Check if two StringDictionary objects are equal or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


