C# Program to find a key in Dictionary

In C#, a Dictionary is a collection that stores key-value pairs. To check if a specific key exists in a Dictionary, you can use the ContainsKey() method, which returns true if the key is found and false otherwise.

Syntax

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

bool result = dictionary.ContainsKey(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() Method

The most straightforward way to check if a key exists in a Dictionary is by using the ContainsKey() method −

using System;
using System.Collections.Generic;

public class Program {
   public static void Main() {
      Dictionary<int, string> d = new Dictionary<int, string>() {
         {1, "Electronics"},
         {2, "Clothing"},
         {3, "Toys"},
         {4, "Footwear"},
         {5, "Accessories"}
      };
      
      foreach (KeyValuePair<int, string> ele in d) {
         Console.WriteLine("Key = {0}, Value = {1}", ele.Key, ele.Value);
      }
      
      Console.WriteLine("Key 5 exists? " + d.ContainsKey(5));
      Console.WriteLine("Key 10 exists? " + d.ContainsKey(10));
   }
}

The output of the above code is −

Key = 1, Value = Electronics
Key = 2, Value = Clothing
Key = 3, Value = Toys
Key = 4, Value = Footwear
Key = 5, Value = Accessories
Key 5 exists? True
Key 10 exists? False

Using TryGetValue() Method

Another approach to check for a key is using TryGetValue(), which both checks if the key exists and retrieves its value in a single operation −

using System;
using System.Collections.Generic;

public class Program {
   public static void Main() {
      Dictionary<string, int> scores = new Dictionary<string, int>() {
         {"Alice", 95},
         {"Bob", 87},
         {"Charlie", 92}
      };
      
      string searchKey = "Bob";
      int value;
      
      if (scores.TryGetValue(searchKey, out value)) {
         Console.WriteLine("Key '{0}' exists with value: {1}", searchKey, value);
      } else {
         Console.WriteLine("Key '{0}' does not exist", searchKey);
      }
      
      searchKey = "David";
      if (scores.TryGetValue(searchKey, out value)) {
         Console.WriteLine("Key '{0}' exists with value: {1}", searchKey, value);
      } else {
         Console.WriteLine("Key '{0}' does not exist", searchKey);
      }
   }
}

The output of the above code is −

Key 'Bob' exists with value: 87
Key 'David' does not exist

Comparison

Method Purpose Performance
ContainsKey() Only checks if key exists O(1) - Single lookup
TryGetValue() Checks key and gets value O(1) - Single lookup with value retrieval

Conclusion

Use ContainsKey() when you only need to verify if a key exists in a Dictionary. For better performance when you also need the value, use TryGetValue() as it performs both the key check and value retrieval in a single operation.

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

394 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements