C# Dictionary ContainsKey() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Dictionary

Definition

In C#, Dictionary.ContainsKey(key) tells you whether a key is in the dictionary. It returns true or false and never throws for a missing key.

The search is very fast. A dictionary uses a hash of the key to jump straight to the right place, so the cost is about O(1), no matter how many pairs are stored.

This is an instance member of Dictionary<TKey,TValue> (System.Collections.Generic). Call it on a dictionary variable, such as ages.ContainsKey(...). A dictionary is a class, so two variables can share one dictionary.

Syntax

Call ContainsKey with the key you want to look for.

C#
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Asha", 25);

bool a = ages.ContainsKey("Asha");   // true
bool b = ages.ContainsKey("Zed");    // false
CallWhat it does
ages.ContainsKey("Asha")true when "Asha" is one of the keys.
ages.ContainsKey("asha")false by default, because string keys are case-sensitive.
new Dictionary<string,int>(StringComparer.OrdinalIgnoreCase)Create the dictionary with a comparer so ContainsKey("asha") is also true.

A null key throws ArgumentNullException. ContainsKey checks keys only; to search values use ContainsValue().

Library declarations
C#
public bool ContainsKey(TKey key);

Parameters

ParameterDescription
keyRequired. The key to look for. It must not be null.

Return Value

Return ValueDescription
booltrue if the dictionary has that key; otherwise false.

C# Examples

Example 1 — Check Three Keys

Look for a key that exists, one that does not, and one with the wrong letter case.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Dictionary<string, int> ages = new Dictionary<string, int>();
        ages.Add("Asha", 25);
        ages.Add("Ravi", 31);

        Console.WriteLine(ages.ContainsKey("Asha"));
        Console.WriteLine(ages.ContainsKey("Zed"));
        Console.WriteLine(ages.ContainsKey("asha"));
    }
}

How It Works

  1. Existing key. "Asha" was added, so the first call returns True.
  2. Missing key. "Zed" was never added, so the second call returns False and does not throw.
  3. Case matters. "asha" is a different string from "Asha", so the default comparer says False.
  4. Values are ignored. Only keys are searched, so the ages 25 and 31 play no part.

Example 2 — Case-Insensitive Keys

Pass StringComparer.OrdinalIgnoreCase to the constructor so upper and lower case match.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Dictionary<string, string> codes =
            new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        codes.Add("IN", "India");
        codes.Add("FR", "France");

        Console.WriteLine(codes.ContainsKey("in"));
        Console.WriteLine(codes.ContainsKey("Fr"));
        Console.WriteLine(codes.ContainsKey("US"));
        Console.WriteLine(codes["fr"]);
    }
}

How It Works

  1. Comparer in the constructor. StringComparer.OrdinalIgnoreCase tells the dictionary to treat "IN" and "in" as the same key.
  2. Lower case hit. ContainsKey("in") finds the key stored as "IN".
  3. Mixed case hit. "Fr" also matches "FR". The unknown "US" is still False.
  4. Indexer follows the same rule. codes["fr"] returns France, because the comparer is used for every lookup.

Example 3 — Guard a Counter with ContainsKey

Count how many times each word appears. A new word starts at 1; a known word goes up by 1.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        string[] words = { "a", "b", "a", "c", "a" };
        Dictionary<string, int> counts = new Dictionary<string, int>();

        foreach (string w in words) {
            if (counts.ContainsKey(w)) {
                counts[w]++;
            } else {
                counts[w] = 1;
            }
        }

        Console.WriteLine(counts["a"]);
        Console.WriteLine(counts["b"]);
        Console.WriteLine(counts.Count);
    }
}

How It Works

  1. Empty counter. The dictionary starts with no words, so the first "a" goes to the else branch and is stored as 1.
  2. Known word. For the second and third "a", ContainsKey is true, so counts[w]++ adds 1 each time.
  3. Final counts. "a" appears 3 times, and "b" and "c" once each.
  4. Three distinct keys. Count is 3 even though the loop ran 5 times. For real code, TryGetValue would do this with one lookup per word.

Frequently Asked Questions

ContainsKey(key) returns true if the dictionary has that key and false if it does not. It does not look at the values.
About O(1), which means the time does not grow with the number of items. That is why a Dictionary is much faster than searching a List for large data.
For string keys, yes, by default. Pass StringComparer.OrdinalIgnoreCase to the Dictionary constructor to ignore case.
Use ContainsKey when you only need yes or no. Use TryGetValue when you also want the value, because it does one lookup instead of two.
ContainsKey throws ArgumentNullException for a null key.

Next: ContainsValue()

Check if any entry has a given value (true or false), by scanning all entries.

ContainsValue() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful