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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.