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
Get or set the value associated with the specified key in StringDictionary in C#
The StringDictionary class in C# provides an indexer that allows you to get or set values associated with specified keys. This indexer uses the key as a parameter and returns or assigns the corresponding value. The StringDictionary is case-insensitive and specifically designed for string keys and values.
Syntax
Following is the syntax for the StringDictionary indexer −
public virtual string this[string key] { get; set; }
To get a value −
string value = stringDictionary[key];
To set a value −
stringDictionary[key] = value;
Parameters
key − The string key whose associated value you want to get or set.
Return Value
Returns the string value associated with the specified key. If the key is not found, it returns null.
Getting Values from StringDictionary
You can retrieve values from a StringDictionary using the indexer with square bracket notation −
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("A", "Books");
strDict.Add("B", "Electronics");
strDict.Add("C", "Appliances");
strDict.Add("D", "Pet Supplies");
strDict.Add("E", "Clothing");
strDict.Add("F", "Footwear");
Console.WriteLine("Value associated with key D = " + strDict["D"]);
Console.WriteLine("Value associated with key F = " + strDict["F"]);
Console.WriteLine("Value associated with key X = " + strDict["X"]);
}
}
The output of the above code is −
Value associated with key D = Pet Supplies Value associated with key F = Footwear Value associated with key X =
Setting Values in StringDictionary
You can also use the indexer to modify existing values or add new key-value pairs −
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("A", "Books");
strDict.Add("B", "Electronics");
strDict.Add("C", "Appliances");
strDict.Add("D", "Pet Supplies");
strDict.Add("E", "Clothing");
strDict.Add("F", "Footwear");
Console.WriteLine("Original value for key F = " + strDict["F"]);
// Update existing value
strDict["F"] = "Sports Equipment";
Console.WriteLine("Updated value for key F = " + strDict["F"]);
// Add new key-value pair using indexer
strDict["G"] = "Home Decor";
Console.WriteLine("New value for key G = " + strDict["G"]);
}
}
The output of the above code is −
Original value for key F = Footwear Updated value for key F = Sports Equipment New value for key G = Home Decor
Case-Insensitive Behavior
The StringDictionary treats keys in a case-insensitive manner. This means keys like "A" and "a" are considered the same −
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("Product", "Laptop");
Console.WriteLine("Value with 'Product' = " + strDict["Product"]);
Console.WriteLine("Value with 'PRODUCT' = " + strDict["PRODUCT"]);
Console.WriteLine("Value with 'product' = " + strDict["product"]);
// Update using different case
strDict["PRODUCT"] = "Desktop";
Console.WriteLine("Updated value with 'Product' = " + strDict["Product"]);
}
}
The output of the above code is −
Value with 'Product' = Laptop Value with 'PRODUCT' = Laptop Value with 'product' = Laptop Updated value with 'Product' = Desktop
Conclusion
The StringDictionary indexer provides a convenient way to access and modify values using string keys. It supports both getting existing values and setting new or updated values, with case-insensitive key matching for enhanced usability.
