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 if the Hashtable contains a specific value in C#
To check if a Hashtable contains a specific value in C#, you use the ContainsValue() method. This method returns true if the specified value exists in the Hashtable, and false otherwise.
Syntax
Following is the syntax for checking if a Hashtable contains a specific value −
bool result = hashtable.ContainsValue(value);
Parameters
value − The value to search for in the Hashtable. It can be
null.
Return Value
Returns true if the Hashtable contains the specified value; otherwise, false.
Using ContainsValue() with Different Data Types
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
Hashtable hash = new Hashtable();
hash.Add("1", "A");
hash.Add("2", "B");
hash.Add("3", "C");
hash.Add("4", "D");
hash.Add("5", "E");
hash.Add("6", "F");
hash.Add("7", "G");
hash.Add("8", "H");
hash.Add("9", "I");
hash.Add("10", "J");
Console.WriteLine("Hashtable Key and Value pairs...");
foreach(DictionaryEntry entry in hash) {
Console.WriteLine("{0} and {1}", entry.Key, entry.Value);
}
Console.WriteLine("\nChecking for specific values:");
Console.WriteLine("Contains value 'H': " + hash.ContainsValue("H"));
Console.WriteLine("Contains value 'Z': " + hash.ContainsValue("Z"));
Console.WriteLine("Contains value 'A': " + hash.ContainsValue("A"));
}
}
The output of the above code is −
Hashtable Key and Value pairs... 10 and J 1 and A 2 and B 3 and C 4 and D 5 and E 6 and F 7 and G 8 and H 9 and I Checking for specific values: Contains value 'H': True Contains value 'Z': False Contains value 'A': True
Checking for Values in a Name Database
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
Hashtable nameDatabase = new Hashtable();
nameDatabase.Add("One", "Katie");
nameDatabase.Add("Two", "John");
nameDatabase.Add("Three", "Barry");
nameDatabase.Add("Four", "Mark");
nameDatabase.Add("Five", "Harry");
nameDatabase.Add("Six", "Nathan");
nameDatabase.Add("Seven", "Tom");
nameDatabase.Add("Eight", "Andy");
nameDatabase.Add("Nine", "Illeana");
nameDatabase.Add("Ten", "Tim");
Console.WriteLine("Name Database:");
foreach(DictionaryEntry entry in nameDatabase) {
Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
}
Console.WriteLine("\nSearch Results:");
Console.WriteLine("Contains 'Illeana': " + nameDatabase.ContainsValue("Illeana"));
Console.WriteLine("Contains 'Sarah': " + nameDatabase.ContainsValue("Sarah"));
Console.WriteLine("Contains 'Tom': " + nameDatabase.ContainsValue("Tom"));
}
}
The output of the above code is −
Name Database: One: Katie Ten: Tim Five: Harry Three: Barry Seven: Tom Two: John Four: Mark Eight: Andy Nine: Illeana Six: Nathan Search Results: Contains 'Illeana': True Contains 'Sarah': False Contains 'Tom': True
ContainsValue() vs ContainsKey() Comparison
| ContainsValue() | ContainsKey() |
|---|---|
| Searches for a value in the Hashtable | Searches for a key in the Hashtable |
| Returns true if value exists | Returns true if key exists |
| Generally slower (O(n) time complexity) | Generally faster (O(1) average time complexity) |
| Useful for reverse lookups | Useful for direct key validation |
Conclusion
The ContainsValue() method provides an easy way to check if a Hashtable contains a specific value. While it's useful for value lookups, remember that it searches through all values sequentially, making it slower than key-based operations like ContainsKey().
