ContainsValue. This C# method searches for a value in a Dictionary. Sometimes we cannot access a Dictionary only by looking up keys—we need to search for a specific value.
With this method, we can locate an entry by its value. ContainsValue is slow. It should not be used unless needed. It eliminates the performance benefits of a Dictionary.
First example. Here we call ContainsValue. When you declare the Dictionary, you specify 2 type parameters. The second type parameter, TValue, is the type used when calling ContainsValue.
using System;
using System.Collections.Generic;
// Step 1: store animal colors.
var animalColors = new Dictionary<string, string>();
animalColors.Add("bird", "blue");
animalColors.Add("worm", "pink");
animalColors.Add("cat", "black");
animalColors.Add("dog", "brown");
// Step 2: see if colors exists.
bool flag1 = animalColors.ContainsValue("black");
bool flag2 = animalColors.ContainsValue("clear");
bool flag3 = animalColors.ContainsValue("blue");
// Step 3: write results.
Console.WriteLine(flag1);
Console.WriteLine(flag2);
Console.WriteLine(flag3);True
False
True
Benchmark. ContainsKey is many times faster than ContainsValue, and this is most apparent in large dictionaries. Consider this benchmark. A dictionary of 1000 key-value pairs is created.
Version 1 This version of the code uses ContainsValue. It repeatedly calls ContainsValue with a key that exists in the Dictionary.
Version 2 Here we use ContainsKey instead. The key exists in the Dictionary, and we access it many times.
Result Checking for the existence of a key (with ContainsKey) is much faster than checking for a value (with ContainsValue).
using System;
using System.Collections.Generic;
using System.Diagnostics;
const int _max = 10000;
// Create a lookup table.
var lookup = new Dictionary<string, string>();
for (int i = 0; i < 1000; i++)
{
lookup[i.ToString() + "a"] = i.ToString() + "b";
}
// Version 1: use ContainsValue.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
if (!lookup.ContainsValue("999b"))
{
return;
}
}
s1.Stop();
// Version 2: use ContainsKey.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
if (!lookup.ContainsKey("999a"))
{
return;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));13388.79 ns ContainsValue
37.49 ns ContainsKey
Discussion. ContainsKey() computes a hash code of the key and use that to locate the value for that key in near-constant time. ContainsValue() loops through all the entries.
And ContainsValue checks the value of each element. For this reason, using ContainsValue is far slower in most cases than ContainsKey.
A summary. With ContainsValue, a slow search of the Dictionary's values (not keys) is performed. This method should be avoided unless needed in a certain situation.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 24, 2023 (rewrite).