C# Dictionary ContainsValue() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Dictionary

Definition

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
CallWhat 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

ParameterDescription
valueRequired. The value to look for. It may be null when TValue is a reference type.

Return Value

Return ValueDescription
booltrue 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);
    }
}

How It Works

  1. Existing value. The pair Ravi=31 is stored, so ContainsValue(31) returns True.
  2. Missing value. No pair has the value 40, so the second call returns False.
  3. Whole scan. To prove 40 is absent, the dictionary had to look at both pairs.
  4. 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"));
    }
}

How It Works

  1. Paris as a key. "Paris" is a capital, not a country, so it is not one of the keys and the result is False.
  2. Paris as a value. The same text is stored as the value of "France", so ContainsValue returns True.
  3. Case-sensitive. String values are compared exactly, so "paris" does not match "Paris".
  4. 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);
    }
}

How It Works

  1. Same value twice. "Asha" and "Meena" are different keys, so the dictionary accepts both even though both have 25.
  2. Yes or no only. ContainsValue(25) returns True but does not say how many entries match.
  3. Manual count. The foreach checks pair.Value for each pair and adds 1 to matches for every 25.
  4. 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.

Next: EnsureCapacity()

Pre-size the Dictionary so it can hold at least a number of entries without growing.

EnsureCapacity() 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.

10 people found this page helpful