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
Check whether the Dictionary contains a specific value or not in C#
The Dictionary<TKey,TValue> class in C# provides the ContainsValue() method to check whether a specific value exists in the dictionary. This method returns true if the value is found, otherwise false.
Syntax
Following is the syntax for the ContainsValue() method −
public bool ContainsValue(TValue value);
Parameters
-
value − The value to locate in the Dictionary<TKey,TValue>.
Return Value
Returns true if the Dictionary<TKey,TValue> contains an element with the specified value; otherwise, false.
Using ContainsValue() - Value Found
The following example demonstrates checking for an existing value 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() - Value Not Found
The following example demonstrates checking for a non-existing value 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!
Multiple Value Checks
You can check for multiple values in a single program to demonstrate different scenarios −
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", 78);
Console.WriteLine("Student Scores:");
foreach(var pair in scores) {
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
int[] searchValues = {95, 100, 87, 60};
Console.WriteLine("\nChecking for specific scores:");
foreach(int score in searchValues) {
if (scores.ContainsValue(score))
Console.WriteLine("Score {0} exists in the dictionary", score);
else
Console.WriteLine("Score {0} does not exist in the dictionary", score);
}
}
}
The output of the above code is −
Student Scores: Alice: 95 Bob: 87 Charlie: 92 Diana: 78 Checking for specific scores: Score 95 exists in the dictionary Score 100 does not exist in the dictionary Score 87 exists in the dictionary Score 60 does not exist in the dictionary
Conclusion
The ContainsValue() method provides an efficient way to check if a specific value exists in a Dictionary<TKey,TValue>. It returns true if the value is found and false otherwise, making it useful for validation and conditional logic in your applications.
