Check whether the Dictionary has the specified key or not in C#

To check whether a Dictionary<TKey,TValue> contains a specified key, C# provides the ContainsKey() method. This method returns true if the key exists in the dictionary, and false otherwise.

Syntax

Following is the syntax for the ContainsKey() method −

public bool ContainsKey(TKey key)

Parameters

  • key − The key to locate in the dictionary.

Return Value

Returns true if the dictionary contains an element with the specified key; otherwise, false.

Using ContainsKey() - Key Found

The following example demonstrates checking for a key that exists in the dictionary −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, string> dict = new Dictionary<string, string>();
      dict.Add("One", "John");
      dict.Add("Two", "Tom");
      dict.Add("Three", "Jacob");
      dict.Add("Four", "Kevin");
      dict.Add("Five", "Nathan");
      
      Console.WriteLine("Count of elements = " + dict.Count);
      Console.WriteLine("\nKey/value pairs...");
      foreach(KeyValuePair<string, string> res in dict) {
         Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
      }
      
      if (dict.ContainsKey("Three"))
         Console.WriteLine("Key 'Three' found!");
      else
         Console.WriteLine("Key 'Three' isn't in the dictionary!");
   }
}

The output of the above code is −

Count of elements = 5

Key/value pairs...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
Key 'Three' found!

Using ContainsKey() - Key Not Found

The following example demonstrates checking for a key that does not exist in the dictionary −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, string> dict = new Dictionary<string, string>();
      dict.Add("One", "John");
      dict.Add("Two", "Tom");
      dict.Add("Three", "Jacob");
      dict.Add("Four", "Kevin");
      dict.Add("Five", "Nathan");
      
      Console.WriteLine("Count of elements = " + dict.Count);
      Console.WriteLine("\nKey/value pairs...");
      foreach(KeyValuePair<string, string> res in dict) {
         Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
      }
      
      if (dict.ContainsKey("mykey"))
         Console.WriteLine("Key 'mykey' found!");
      else
         Console.WriteLine("Key 'mykey' isn't in the dictionary!");
   }
}

The output of the above code is −

Count of elements = 5

Key/value pairs...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
Key 'mykey' isn't in the dictionary!

Practical Usage with TryGetValue()

If you need both to check for a key and retrieve its value, TryGetValue() is more efficient than using ContainsKey() followed by indexing −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, int> scores = new Dictionary<string, int>();
      scores.Add("Alice", 95);
      scores.Add("Bob", 87);
      scores.Add("Charlie", 92);
      
      string playerName = "Bob";
      if (scores.TryGetValue(playerName, out int score)) {
         Console.WriteLine($"Player {playerName} has a score of {score}");
      } else {
         Console.WriteLine($"Player {playerName} not found");
      }
      
      playerName = "David";
      if (scores.TryGetValue(playerName, out score)) {
         Console.WriteLine($"Player {playerName} has a score of {score}");
      } else {
         Console.WriteLine($"Player {playerName} not found");
      }
   }
}

The output of the above code is −

Player Bob has a score of 87
Player David not found

Conclusion

The ContainsKey() method is the standard way to check if a dictionary contains a specific key in C#. For scenarios where you need both to check existence and retrieve the value, TryGetValue() provides better performance by combining both operations into a single method call.

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

214 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements