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
Add key and value into StringDictionary in C#
The StringDictionary class in C# provides a specialized collection for storing string key-value pairs. It is case-insensitive, meaning keys are automatically converted to lowercase when added. The Add() method is used to insert new key-value pairs into the collection.
Syntax
Following is the syntax for adding key-value pairs to a StringDictionary −
StringDictionary dictionary = new StringDictionary(); dictionary.Add(key, value);
Parameters
-
key − The key to add to the StringDictionary (string type).
-
value − The value associated with the key (string type).
Key Rules
-
All keys are automatically converted to lowercase when added.
-
Keys must be unique − adding a duplicate key throws an exception.
-
Both keys and values must be strings.
-
The collection is case-insensitive for key lookups.
Using Add() Method
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
Adding Numeric String Keys
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("1", "Electric Cars");
strDict.Add("2", "SUV");
strDict.Add("3", "AUV");
Console.WriteLine("StringDictionary elements...");
foreach(DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
The output of the above code is −
StringDictionary elements... 1 Electric Cars 2 SUV 3 AUV
Checking for Existing Keys
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("Name", "Alice");
strDict.Add("Age", "25");
// Check if key exists before adding
if (!strDict.ContainsKey("City")) {
strDict.Add("City", "New York");
Console.WriteLine("City added successfully");
}
// This will check for lowercase key
Console.WriteLine("Contains 'name': " + strDict.ContainsKey("name"));
Console.WriteLine("Contains 'NAME': " + strDict.ContainsKey("NAME"));
Console.WriteLine("Count: " + strDict.Count);
}
}
The output of the above code is −
City added successfully Contains 'name': True Contains 'NAME': True Count: 3
Conclusion
The Add() method in StringDictionary allows you to insert string key-value pairs into a case-insensitive collection. Remember that all keys are automatically converted to lowercase, and duplicate keys will cause an exception to be thrown.
