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.

StringDictionary Key Conversion Input Keys "A", "B", "C" "Name", "Age" "ID", "Email" Stored Keys "a", "b", "c" "name", "age" "id", "email" All keys automatically converted to lowercase

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.

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

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements