C# | StringDictionary Class
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.
Constructors
Constructor | Description |
---|---|
StringDictionary() | Initializes a new instance of the StringDictionary class. |
Example:
// C# code to create a 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( "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
Properties
Property | Description |
---|---|
Count | Gets the number of key/value pairs in the StringDictionary. |
IsSynchronized | Gets a value indicating whether access to the StringDictionary is synchronized (thread safe). |
Item[String] | Gets or sets the value associated with the specified key. |
Keys | Gets a collection of keys in the StringDictionary. |
SyncRoot | Gets an object that can be used to synchronize access to the StringDictionary. |
Values | Gets a collection of values in the StringDictionary. |
Example:
// C# code illustrate the Properties of // StringDictionary class 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( "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
Methods
Method | Description |
---|---|
Add(String, String) | Adds an entry with the specified key and value into the StringDictionary. |
Clear() | Removes all entries from the StringDictionary. |
ContainsKey(String) | Determines if the StringDictionary contains a specific key. |
ContainsValue(String) | Determines if the StringDictionary contains a specific value. |
CopyTo(Array, Int32) | Copies the string dictionary values to a one-dimensional Array instance at the specified index. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
GetEnumerator() | Returns an enumerator that iterates through the string dictionary. |
GetHashCode() | Serves as the default hash function. |
GetType() | Gets the Type of the current instance. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
Remove(String) | Removes the entry with the specified key from the string dictionary. |
ToString() | Returns a string that represents the current object. |
Example 1:
// C# code to remove the entry // with the specified key from // the 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( "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 index 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( "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