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
.
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
.
Dictionary
containing keys and values of string
type is created. It is populated with animal names and their default colors.ContainsValue
.ContainsValue()
searches all entries in the Dictionary
for a match and returns a bool
.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
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.
ContainsValue
. It repeatedly calls ContainsValue
with a key that exists in the Dictionary
.ContainsKey
instead. The key exists in the Dictionary
, and we access it many times.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
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.
ContainsValue
checks the value of each element. For this reason, using ContainsValue
is far slower in most cases than ContainsKey
.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.