Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Dictionary.ContainsValue() Method in C#
The Dictionary.ContainsValue() method in C# is used to check whether the Dictionary<TKey,TValue> contains a specific value or not. This method returns true if the value is found in the dictionary, otherwise it returns false.
Syntax
public bool ContainsValue(TValue val);
Parameters
val − The value to search for in the dictionary. This parameter can be null if the value type allows null values.
Return Value
Returns true if the dictionary contains the specified value; otherwise, false.
Using ContainsValue() with Found Value
The following example demonstrates checking for a value that exists in the dictionary −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("One", "John");
dict.Add("Two", "Tom");
dict.Add("Three", "Jacob");
dict.Add("Four", "Kevin");
dict.Add("Five", "Nathan");
Console.WriteLine("Count of elements = " + dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
if (dict.ContainsValue("Kevin"))
Console.WriteLine("Value 'Kevin' found!");
else
Console.WriteLine("Value 'Kevin' isn't in the dictionary!");
}
}
The output of the above code is −
Count of elements = 5 Key/value pairs... Key = One, Value = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan Value 'Kevin' found!
Using ContainsValue() with Missing Value
The following example demonstrates checking for a value that does not exist in the dictionary −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("One", "Chris");
dict.Add("Two", "Steve");
dict.Add("Three", "Messi");
dict.Add("Four", "Ryan");
dict.Add("Five", "Nathan");
Console.WriteLine("Count of elements = " + dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
if (dict.ContainsValue("Angelina"))
Console.WriteLine("Value 'Angelina' found!");
else
Console.WriteLine("Value 'Angelina' isn't in the dictionary!");
}
}
The output of the above code is −
Count of elements = 5 Key/value pairs... Key = One, Value = Chris Key = Two, Value = Steve Key = Three, Value = Messi Key = Four, Value = Ryan Key = Five, Value = Nathan Value 'Angelina' isn't in the dictionary!
Using ContainsValue() with Different Data Types
The ContainsValue() method works with any data type. Here's an example using integer values −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, int> scores = new Dictionary<string, int>();
scores.Add("Alice", 95);
scores.Add("Bob", 87);
scores.Add("Charlie", 92);
scores.Add("Diana", 88);
Console.WriteLine("Student scores:");
foreach(KeyValuePair<string, int> score in scores){
Console.WriteLine("{0}: {1}", score.Key, score.Value);
}
int searchScore = 92;
if (scores.ContainsValue(searchScore))
Console.WriteLine("Score {0} found in the dictionary!", searchScore);
else
Console.WriteLine("Score {0} not found in the dictionary!", searchScore);
}
}
The output of the above code is −
Student scores: Alice: 95 Bob: 87 Charlie: 92 Diana: 88 Score 92 found in the dictionary!
How It Works
The ContainsValue() method performs a linear search through all values in the dictionary. It uses the default equality comparer for the value type to determine if a match exists. For reference types, it compares object references, while for value types, it compares the actual values.
Time Complexity: O(n) where n is the number of elements in the dictionary, as it may need to check every value.
Conclusion
The Dictionary.ContainsValue() method provides a simple way to check if a specific value exists in a dictionary. It returns a boolean result and works with any data type, making it useful for validation and conditional logic in C# applications.
