Gets or sets the value at the specified key in StringDictionary in C#

The StringDictionary class in C# provides an indexer property that allows you to get or set values using a key-based approach. This indexer uses the syntax dictionary[key] to access or modify values at the specified key.

Syntax

Following is the syntax for getting or setting values using the indexer −

// Getting a value
string value = stringDictionary[key];

// Setting a value
stringDictionary[key] = newValue;

Parameters

  • key − A string that represents the key to locate in the StringDictionary.

  • value − The string value to associate with the specified key when setting.

Return Value

When getting a value, it returns the string value associated with the specified key. If the key does not exist, it returns null.

Getting Values from StringDictionary

The following example demonstrates how to retrieve values using the indexer −

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("Value for key 3 = " + myDict["3"]);
   }
}

The output of the above code is −

Value for key 5 = Notebook
Value for key 3 = Speakers

Setting Values in StringDictionary

The indexer can also be used to update existing values or add new key-value pairs −

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");
      
      Console.WriteLine("Original value for key 5 = " + myDict["5"]);
      
      // Update existing value
      myDict["5"] = "Flash Drive";
      Console.WriteLine("Updated value for key 5 = " + myDict["5"]);
      
      // Add new key-value pair
      myDict["6"] = "Monitor";
      Console.WriteLine("New value for key 6 = " + myDict["6"]);
   }
}

The output of the above code is −

Original value for key 5 = Notebook
Updated value for key 5 = Flash Drive
New value for key 6 = Monitor

Handling Non-Existent Keys

When accessing a key that doesn't exist, the indexer returns null

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");
      
      Console.WriteLine("Value for existing key 1: " + myDict["1"]);
      
      string nonExistentValue = myDict["99"];
      if (nonExistentValue == null) {
         Console.WriteLine("Key 99 does not exist - returned null");
      }
      
      // Setting value for non-existent key creates new entry
      myDict["99"] = "New Device";
      Console.WriteLine("Value for key 99 after setting: " + myDict["99"]);
   }
}

The output of the above code is −

Value for existing key 1: Tablet
Key 99 does not exist - returned null
Value for key 99 after setting: New Device

Conclusion

The StringDictionary indexer provides a convenient way to access and modify values using string keys. It returns null for non-existent keys when getting values, and automatically creates new entries when setting values for keys that don't exist.

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

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements