C# StringDictionary Class (original) (raw)

Last Updated : 06 Feb, 2025

In C#, the **StringDictionary class is the part of the System.Collections.Specialized namespace. It is a collection of key-value pairs where the keys are strings and values are objects. It is similar to a Hashtable. But it is specifically designed for use with string keys.

**Example: This example demonstrates how to use the StringDictionary class to store and iterate over key-value pairs where keys are strings and values can be any object.

C# `

// C# program to demonstrate StringDictionary class using System; using System.Collections; using System.Collections.Specialized;

class Geeks { static void Main() { // Create a StringDictionary StringDictionary sd = new StringDictionary();

    // Insert key-value pairs
    sd.Add("1", "Geek1");
    sd.Add("2", "Geek2");
    sd.Add("3", "Geek3");
    sd.Add("4", "Geek4");

    // Iterate through the StringDictionary 
    // and print the key-value pairs
    foreach (DictionaryEntry i in sd)
    {
        Console.WriteLine($"{i.Key} --> {i.Value}");
    }
}

}

`

Output

3 --> Geek3 4 --> Geek4 2 --> Geek2 1 --> Geek1

Declaration of StringDictionary

In C#, the StringDictionary is declared as:

StringDictionary dictionary = new StringDictionary();

Constructor

**StringDictionary(): Initializes a new instance of the StringDictionary class.

**Example: This example demonstrates how to create a StringDictionary, add integer values as string, and iterate through the dictionary to display the keys and their corresponding values.

C# `

// C# program to demonstrates how to // create a StringDictionary using System; using System.Collections; using System.Collections.Specialized;

class Geeks {

public static void Main() 
{ 

    // Creating a StringDictionary
    StringDictionary sd = new StringDictionary(); 

    // Adding key and value into the StringDictionary 
    sd.Add("X", "100"); 
    sd.Add("Y", "200"); 
    sd.Add("Z", "300"); 

    // Displaying the keys and values in StringDictionary 
    foreach(DictionaryEntry i in sd) 
    { 
        Console.WriteLine(i.Key + " --> " + i.Value); 
    } 
} 

}

`

Output

x --> 100 z --> 300 y --> 200

Properties

Properties 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: This example demonstrates how to use Values property to access that values in StringDictionary and check its IsSynchronized property to determine if its thread-safe.

C# `

// C# program to demonstrates the working of // values and IsSynchronized property using System; using System.Collections; using System.Collections.Specialized;

class Geeks{

public static void Main() {

    // Creating a StringDictionary
    StringDictionary sd = new StringDictionary();

    // Adding key and value 
    sd.Add("1", "One");
    sd.Add("5", "Five");
    sd.Add("7", "Seven");
    sd.Add("10", "Ten");

    // Values Property

    // Getting a collection of values in the StringDictionary
    foreach (string i in sd.Values) {
        Console.WriteLine(i);
    }

    // IsSynchronized Property

    // Checking if StringDictionary is synchronized(thread-safe)
    Console.WriteLine(sd.IsSynchronized);
}

}

`

Output

Five Seven Ten One False

Methods

Methods 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: This example demonstrates how to use the Count property to get the number of key-value pairs and how to remove key-value pair using the Remove() method.

C# `

// C# code to remove the entry // with the specified key from // the StringDictionary using System; using System.Collections; using System.Collections.Specialized;

class Geeks {

public static void Main() 
{ 

    // Creating a StringDictionary 
    StringDictionary sd = new StringDictionary(); 

    // Adding key and value into the StringDictionary 
    sd.Add("X", "100"); 
    sd.Add("Y", "200"); 
    sd.Add("Z", "300"); 

    // Displaying the keys and values in StringDictionary 
    Console.WriteLine("The number of key-value pairs are : " + sd.Count); 

    // Removing the entry with the specified 
    // key from the StringDictionary 
    sd.Remove("Y"); 

    // Displaying the keys and values in StringDictionary 
    Console.WriteLine("The number of key-value pairs are : " + sd.Count); 
} 

}

`

Output

The number of key-value pairs are : 3 The number of key-value pairs are : 2

**Example 2: This example demonstrates how to use the CopyTo method to copy the contents of a StringDicitonary into an array and then display the key-value pairs from the array.

C# `

// C# code to copy StringDictionary // to Array at the specified index using System; using System.Collections; using System.Collections.Specialized;

class Geeks {

public static void Main()
{

    // Creating a StringDictionary
    StringDictionary sd = new StringDictionary();

    // Adding key and value into the StringDictionary
    sd.Add("1", "Geeks");
    sd.Add("2", "for");
    sd.Add("3", "Geeks");

    // Creating an Array
    DictionaryEntry[] arr = { new DictionaryEntry(),
                              new DictionaryEntry(),
                              new DictionaryEntry() };

    // Copying StringDictionary to
    // Array at the specified index
    sd.CopyTo(arr, 0);

    // Displaying key and value pairs in Array
    for (int i = 0; i < arr.Length; i++) {
        Console.WriteLine(arr[i].Key + " --> "
                          + arr[i].Value);
    }
}

}

`

Output

3 --> Geeks 2 --> for 1 --> Geeks