Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a StringDictionary in C#?
The StringDictionary in C# is a specialized collection class from the System.Collections.Specialized namespace that stores key-value pairs where both keys and values are strings. It's important to note that StringDictionary is case-insensitive and automatically converts all keys to lowercase.
Note: StringDictionary is considered legacy and is generally replaced by Dictionary<string, string> in modern C# applications. However, understanding StringDictionary is useful for maintaining older codebases.
Syntax
Following is the syntax for creating and using a StringDictionary −
StringDictionary stringDict = new StringDictionary();
stringDict.Add("key", "value");
string value = stringDict["key"];
Creating a Basic StringDictionary
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("A", "John");
strDict.Add("B", "Andy");
strDict.Add("C", "Tim");
strDict.Add("D", "Ryan");
strDict.Add("E", "Kevin");
strDict.Add("F", "Katie");
strDict.Add("G", "Brad");
Console.WriteLine("StringDictionary elements...");
foreach(DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
The output of the above code is −
StringDictionary elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad
Accessing Values by Key
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary myDict = new StringDictionary();
myDict.Add("1", "Tablet");
myDict.Add("2", "Desktop");
myDict.Add("3", "Speakers");
myDict.Add("4", "Laptop");
myDict.Add("5", "Notebook");
myDict.Add("6", "Ultrabook");
myDict.Add("7", "HDD");
myDict.Add("8", "SSD");
myDict.Add("9", "Headphone");
myDict.Add("10", "Earphone");
Console.WriteLine("Value for key 5 = " + myDict["5"]);
Console.WriteLine("Total items: " + myDict.Count);
}
}
The output of the above code is −
Value for key 5 = Notebook Total items: 10
Common Operations
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary dict = new StringDictionary();
// Adding items
dict.Add("Name", "Alice");
dict.Add("AGE", "25"); // Key will be stored as "age"
dict.Add("city", "New York");
// Accessing with different case - all work the same
Console.WriteLine("Name: " + dict["name"]); // Works
Console.WriteLine("Age: " + dict["AGE"]); // Works
Console.WriteLine("City: " + dict["CITY"]); // Works
// Check if key exists
Console.WriteLine("Contains 'NAME': " + dict.ContainsKey("NAME"));
// Remove an item
dict.Remove("age");
Console.WriteLine("Count after removal: " + dict.Count);
}
}
The output of the above code is −
Name: Alice Age: 25 City: New York Contains 'NAME': True Count after removal: 2
StringDictionary vs Dictionary<string, string>
| StringDictionary | Dictionary<string, string> |
|---|---|
| Case-insensitive keys (auto-converted to lowercase) | Case-sensitive keys by default |
| Legacy class from .NET Framework 1.1 | Modern generic collection (recommended) |
| Only string keys and values | Strongly-typed with compile-time checking |
| Uses DictionaryEntry for enumeration | Uses KeyValuePair<string, string> |
Conclusion
StringDictionary in C# provides a case-insensitive way to store string key-value pairs, automatically converting all keys to lowercase. While functional, modern C# applications should prefer Dictionary<string, string> for better performance and type safety, using StringComparer.OrdinalIgnoreCase when case-insensitive behavior is needed.
