C# | StringDictionary Class - GeeksforGeeks

24 Jan, 2019
StringDictionary is a specialized collection. This class comes under 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.
Characteristics:
A key cannot be null, but a value can.
The key is handled in a case-insensitive manner i.e, it is translated to lowercase before it is used with the string dictionary.
The constructor StringDictionary() initializes a new instance of the StringDictionary class.
Example:
// C# code to create a 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("A", "Apple");        myDict.Add("B", "Banana");        myDict.Add("C", "Cat");        myDict.Add("D", "Dog");        myDict.Add("E", "Elephant");          // Displaying the keys and values in StringDictionary        foreach(DictionaryEntry dic in myDict)        {            Console.WriteLine(dic.Key + " " + dic.Value);        }    }}
Output:
d Dog
b Banana
c Cat
e Elephant
a Apple

Example:
// C# code illustrate the Properties of// StringDictionary classusing 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("3", "prime & odd");         myDict.Add("2", "prime & even");         myDict.Add("4", "non-prime & even");         myDict.Add("9", "non-prime & odd");                 // -------- Values Property --------                  // Getting a collection of values         // in the StringDictionary         foreach(string val in myDict.Values)         {             Console.WriteLine(val);         }                             // -------- IsSynchronized Property --------                  // Checking if StringDictionary         // is synchronized (thread safe)         Console.WriteLine(myDict.IsSynchronized);     } } 
Output:
prime & even
prime & odd
non-prime & odd
non-prime & even
False

Example 1:
// C# code to remove the entry// with the specified key from// the 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("A", "Apple");        myDict.Add("B", "Banana");        myDict.Add("C", "Cat");        myDict.Add("D", "Dog");        myDict.Add("E", "Elephant");        myDict.Add("F", "Fish");          // Displaying the keys and values in StringDictionary        Console.WriteLine("The number of key/value pairs are : " + myDict.Count);          // Removing the entry with the specified        // key from the StringDictionary        myDict.Remove("D");          // Displaying the keys and values in StringDictionary        Console.WriteLine("The number of key/value pairs are : " + myDict.Count);    }}
Output:
The number of key/value pairs are : 6
The number of key/value pairs are : 5

Example 2:
// C# code to copy StringDictionary// to Array at the specified indexusing 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("A", "Apple");        myDict.Add("B", "Banana");        myDict.Add("C", "Cat");        myDict.Add("D", "Dog");        myDict.Add("E", "Elephant");          // Creating an Array named myArr        DictionaryEntry[] myArr = { new DictionaryEntry(),                                    new DictionaryEntry(),                                    new DictionaryEntry(),                                    new DictionaryEntry(),                                    new DictionaryEntry() };          // Copying StringDictionary to        // Array at the specified index        myDict.CopyTo(myArr, 0);          // Displaying key and value pairs in Array myArr        for (int i = 0; i < myArr.Length; i++) {            Console.WriteLine(myArr[i].Key + " " + myArr[i].Value);        }    }}
Output:
d Dog
b Banana
c Cat
e Elephant
a Apple

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringdictionary?view=netframework-4.7.2
CSharp-Collections-Namespace
CSharp-Specialized-Namespace
CSharp-Specialized-StringDictionary
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Method Overriding
C# Dictionary with examples
Difference between Ref and Out keywords in C#
C# | Constructors
C# | Class and Object
C# | Delegates
Introduction to .NET Framework
Extension Method in C#
C# | Abstract Classes
C# | Data Types