Check if the StringDictionary contains a specific key in C#

The StringDictionary class in C# provides the ContainsKey() method to check if a specific key exists in the dictionary. This method returns true if the key is found, otherwise false. Note that StringDictionary automatically converts keys to lowercase for storage and comparison.

Syntax

Following is the syntax for using the ContainsKey() method −

bool ContainsKey(string key)

Parameters

  • key − The string key to search for in the StringDictionary.

Return Value

Returns true if the StringDictionary contains the specified key; otherwise, false.

Using ContainsKey() Method

Example 1

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");
      
      Console.WriteLine("StringDictionary elements...");
      foreach(DictionaryEntry d in strDict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      // Check for existing key
      Console.WriteLine("Does StringDictionary have key 'C'? " + strDict.ContainsKey("C"));
      
      // Check for non-existing key
      Console.WriteLine("Does StringDictionary have key 'Z'? " + strDict.ContainsKey("Z"));
      
      // Case sensitivity test
      Console.WriteLine("Does StringDictionary have key 'c' (lowercase)? " + strDict.ContainsKey("c"));
   }
}

The output of the above code is −

StringDictionary elements...
a John
b Andy
c Tim
d Ryan
e Kevin
Does StringDictionary have key 'C'? True
Does StringDictionary have key 'Z'? False
Does StringDictionary have key 'c' (lowercase)? True

Case Insensitivity in StringDictionary

StringDictionary is case-insensitive, meaning it converts all keys to lowercase internally. This affects how ContainsKey() works −

Example 2

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringDictionary dict = new StringDictionary();
      dict.Add("Name", "Alice");
      dict.Add("AGE", "25");
      dict.Add("City", "New York");
      
      Console.WriteLine("Testing case insensitive key checks:");
      Console.WriteLine("ContainsKey('Name'): " + dict.ContainsKey("Name"));
      Console.WriteLine("ContainsKey('name'): " + dict.ContainsKey("name"));
      Console.WriteLine("ContainsKey('NAME'): " + dict.ContainsKey("NAME"));
      Console.WriteLine("ContainsKey('age'): " + dict.ContainsKey("age"));
      Console.WriteLine("ContainsKey('CITY'): " + dict.ContainsKey("CITY"));
      
      // Show actual stored keys (all lowercase)
      Console.WriteLine("\nActual keys stored:");
      foreach(string key in dict.Keys) {
         Console.WriteLine("'" + key + "'");
      }
   }
}

The output of the above code is −

Testing case insensitive key checks:
ContainsKey('Name'): True
ContainsKey('name'): True
ContainsKey('NAME'): True
ContainsKey('age'): True
ContainsKey('CITY'): True

Actual keys stored:
'name'
'age'
'city'

Practical Usage Pattern

Example 3

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringDictionary userSettings = new StringDictionary();
      userSettings.Add("theme", "dark");
      userSettings.Add("language", "english");
      userSettings.Add("notifications", "enabled");
      
      // Safe key checking before accessing values
      string[] keysToCheck = {"theme", "font", "language", "volume"};
      
      foreach(string key in keysToCheck) {
         if(userSettings.ContainsKey(key)) {
            Console.WriteLine(key + ": " + userSettings[key]);
         } else {
            Console.WriteLine(key + ": Not found");
         }
      }
   }
}

The output of the above code is −

theme: dark
font: Not found
language: english
volume: Not found

Conclusion

The ContainsKey() method in StringDictionary provides an efficient way to check for key existence before accessing values. Remember that StringDictionary is case-insensitive, so all key comparisons are done in lowercase, making it suitable for scenarios where case doesn't matter.

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

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements