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
Call
What 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
Parameter
Description
key
Required. The key to look for. It must not be null.
Return Value
Return Value
Description
bool
true 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"));
}
}
Output
True
False
False
How It Works
Existing key."Asha" was added, so the first call returns True.
Missing key."Zed" was never added, so the second call returns False and does not throw.
Case matters."asha" is a different string from "Asha", so the default comparer says False.
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"]);
}
}
Output
True
True
False
France
How It Works
Comparer in the constructor.StringComparer.OrdinalIgnoreCase tells the dictionary to treat "IN" and "in" as the same key.
Lower case hit.ContainsKey("in") finds the key stored as "IN".
Mixed case hit."Fr" also matches "FR". The unknown "US" is still False.
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);
}
}
Output
3
1
3
How It Works
Empty counter. The dictionary starts with no words, so the first "a" goes to the else branch and is stored as 1.
Known word. For the second and third "a", ContainsKey is true, so counts[w]++ adds 1 each time.
Final counts."a" appears 3 times, and "b" and "c" once each.
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.