In C#, Dictionary.ContainsValue(value) checks whether any pair holds the given value. It returns true or false.
A dictionary is built for fast lookup by key, not by value. To answer this question, ContainsValue looks at every entry one by one, so it costs O(n). That is fine for small data, but slow for very large dictionaries that you check often.
This is an instance member of Dictionary<TKey,TValue> (System.Collections.Generic). Call it on a dictionary variable, such as ages.ContainsValue(...). A dictionary is a class, so two variables can share one dictionary.
Syntax
Call ContainsValue with the value you want to find.
C#
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Asha", 25);
ages.Add("Ravi", 31);
bool a = ages.ContainsValue(31); // true
bool b = ages.ContainsValue(40); // false
Call
What it does
ages.ContainsValue(31)
true when some key is linked to 31.
ages.ContainsKey("Ravi")
Fast O(1) check by key. Not the same question.
foreach (var kv in ages) { ... }
Loop when you need to know which key owns the value.
The null value is allowed for reference types, so ContainsValue(null) works. Values are compared with the default equality for TValue, so strings are case-sensitive.
Library declarations
C#
public bool ContainsValue(TValue value);
Parameters
Parameter
Description
value
Required. The value to look for. It may be null when TValue is a reference type.
Return Value
Return Value
Description
bool
true if at least one entry has that value; otherwise false.
C# Examples
Example 1 — Check Two Values
Ask whether the dictionary contains a value that exists and one that does not.
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.ContainsValue(31));
Console.WriteLine(ages.ContainsValue(40));
Console.WriteLine(ages.Count);
}
}
Output
True
False
2
How It Works
Existing value. The pair Ravi=31 is stored, so ContainsValue(31) returns True.
Missing value. No pair has the value 40, so the second call returns False.
Whole scan. To prove 40 is absent, the dictionary had to look at both pairs.
Nothing changes.Count is still 2, because ContainsValue only reads.
Example 2 — ContainsKey Is Not ContainsValue
Use a country and capital dictionary to see that the two methods answer different questions.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Dictionary<string, string> capitals = new Dictionary<string, string>();
capitals.Add("India", "Delhi");
capitals.Add("France", "Paris");
Console.WriteLine(capitals.ContainsKey("Paris"));
Console.WriteLine(capitals.ContainsValue("Paris"));
Console.WriteLine(capitals.ContainsValue("paris"));
Console.WriteLine(capitals.ContainsKey("India"));
}
}
Output
False
True
False
True
How It Works
Paris as a key."Paris" is a capital, not a country, so it is not one of the keys and the result is False.
Paris as a value. The same text is stored as the value of "France", so ContainsValue returns True.
Case-sensitive. String values are compared exactly, so "paris" does not match "Paris".
India as a key."India" really is a key, so ContainsKey returns True.
Example 3 — Count How Many Entries Share a Value
Keys must be unique, but values can repeat. Use a loop to count the entries that have age 25.
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);
ages.Add("Meena", 25);
Console.WriteLine(ages.ContainsValue(25));
int matches = 0;
foreach (KeyValuePair<string, int> pair in ages) {
if (pair.Value == 25) {
matches++;
}
}
Console.WriteLine(matches);
}
}
Output
True
2
How It Works
Same value twice."Asha" and "Meena" are different keys, so the dictionary accepts both even though both have 25.
Yes or no only.ContainsValue(25) returns True but does not say how many entries match.
Manual count. The foreach checks pair.Value for each pair and adds 1 to matches for every 25.
Order does not matter. The total is 2 whichever order the pairs are visited in.
Frequently Asked Questions
It returns true if at least one entry in the dictionary has the given value, and false if none does.
It checks every entry, so it is O(n). For large dictionaries that you search by value often, keep a second dictionary in the other direction.
ContainsKey searches the keys and is very fast. ContainsValue searches the values and has to scan all entries.
ContainsValue only says yes or no. Loop over the pairs with foreach and compare pair.Value to get the matching key or keys.
For strings, yes. "Paris" and "paris" are different values unless you compare them yourself with a loop and a case-insensitive check.