StringDictionary Class in C#?

The StringDictionary class implements a hash table with the key and the value strongly typed to be strings rather than objects. It is part of the System.Collections.Specialized namespace and provides a case-insensitive string-based collection.

Note: The StringDictionary class is considered obsolete. For new development, use Dictionary<string, string> which provides better performance and more features.

Syntax

Following is the basic syntax to create and use a StringDictionary −

StringDictionary stringDict = new StringDictionary();
stringDict.Add("key", "value");
string value = stringDict["key"];

Properties

Following are the key properties of the StringDictionary class −

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.

Methods

Following are the commonly used methods of the StringDictionary class −

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.
Remove(String) Removes the entry with the specified key from the StringDictionary.
CopyTo(Array, Int32) Copies the string dictionary values to a one-dimensional Array instance at the specified index.

Using StringDictionary Basic Operations

Example

using System;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringDictionary strDict = new StringDictionary();
        
        // Adding key-value pairs
        strDict.Add("A", "Apple");
        strDict.Add("B", "Banana");
        strDict.Add("C", "Cherry");
        
        Console.WriteLine("Count: " + strDict.Count);
        Console.WriteLine("Value for key 'B': " + strDict["B"]);
        Console.WriteLine("Contains key 'A': " + strDict.ContainsKey("A"));
        Console.WriteLine("Contains value 'Cherry': " + strDict.ContainsValue("Cherry"));
        
        // Display all key-value pairs
        Console.WriteLine("\nAll key-value pairs:");
        foreach(string key in strDict.Keys) {
            Console.WriteLine(key + " = " + strDict[key]);
        }
    }
}

The output of the above code is −

Count: 3
Value for key 'B': Banana
Contains key 'A': True
Contains value 'Cherry': True

All key-value pairs:
a = Apple
b = Banana
c = Cherry

Using StringDictionary Comparison

Example

using System;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringDictionary strDict1 = new StringDictionary();
        strDict1.Add("A", "John");
        strDict1.Add("B", "Andy");
        strDict1.Add("C", "Tim");
        
        StringDictionary strDict2 = new StringDictionary();
        strDict2.Add("A", "John");
        strDict2.Add("B", "Andy");
        strDict2.Add("C", "Tim");
        
        StringDictionary strDict3 = new StringDictionary();
        strDict3 = strDict2;
        
        Console.WriteLine("Is Dictionary2 equal to Dictionary3? = " + strDict2.Equals(strDict3));
        Console.WriteLine("Is Dictionary1 equal to Dictionary2? = " + strDict1.Equals(strDict2));
        Console.WriteLine("IsSynchronized: " + strDict1.IsSynchronized);
    }
}

The output of the above code is −

Is Dictionary2 equal to Dictionary3? = True
Is Dictionary1 equal to Dictionary2? = False
IsSynchronized: False

Case-Insensitive Nature

Example

using System;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringDictionary strDict = new StringDictionary();
        
        // Keys are case-insensitive
        strDict.Add("Name", "Alice");
        strDict.Add("Age", "25");
        
        Console.WriteLine("Using 'Name': " + strDict["Name"]);
        Console.WriteLine("Using 'name': " + strDict["name"]);
        Console.WriteLine("Using 'NAME': " + strDict["NAME"]);
        
        // All keys are stored in lowercase
        Console.WriteLine("\nActual keys stored:");
        foreach(string key in strDict.Keys) {
            Console.WriteLine("'" + key + "'");
        }
    }
}

The output of the above code is −

Using 'Name': Alice
Using 'name': Alice
Using 'NAME': Alice

Actual keys stored:
'name'
'age'

StringDictionary vs Dictionary<string, string>

StringDictionary Dictionary<string, string>
Case-insensitive keys by default Case-sensitive keys by default
Legacy class, considered obsolete Modern generic collection
Keys stored in lowercase Keys stored as provided
Less performance optimized Better performance

Conclusion

The StringDictionary class provides a case-insensitive hash table for string key-value pairs. While functional, it's considered obsolete and Dictionary<string, string> with StringComparer.OrdinalIgnoreCase is recommended for new applications due to better performance and more features.

Updated on: 2026-03-17T07:04:36+05:30

275 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements