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 a Hashtable contains a specific key or not in C#
The Hashtable class in C# provides the Contains() method to check whether a specific key exists in the collection. This method returns true if the key is found, and false otherwise.
Syntax
Following is the syntax for checking if a Hashtable contains a specific key −
bool result = hashtable.Contains(key);
Parameters
-
key − The key to search for in the Hashtable. This parameter is of type
Object.
Return Value
The Contains() method returns a bool value −
-
trueif the Hashtable contains the specified key -
falseif the key is not found
Using Contains() Method
Example
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash = new Hashtable();
hash.Add("One", "Katie");
hash.Add("Two", "John");
hash.Add("Three", "Barry");
hash.Add("Four", "");
hash.Add("Five","Harry");
hash.Add("Six", "F");
hash.Add("Seven", "Tom");
hash.Add("Eight","Andy");
hash.Add("Nine", "I");
hash.Add("Ten", "Tim");
Console.WriteLine("Hashtable Key and Value pairs...");
foreach(DictionaryEntry entry in hash){
Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
}
Console.WriteLine("Is the Hashtable having fixed size? = "+hash.IsFixedSize);
Console.WriteLine("If Hashtable read-only? = "+hash.IsReadOnly);
Hashtable hash2 = Hashtable.Synchronized(hash);
Console.WriteLine("Is Hash synchronized = "+hash2.IsSynchronized);
Console.WriteLine("Does Hashtable contains the key Ten = "+hash.Contains("Ten"));
}
}
The output of the above code is −
Hashtable Key and Value pairs... One and Katie Ten and Tim Five and Harry Three and Barry Seven and Tom Two and John Four and Eight and Andy Nine and I Six and F Is the Hashtable having fixed size? = False If Hashtable read-only? = False Is Hash synchronized = True Does Hashtable contains the key Ten = True
Checking Multiple Keys
Example
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash = new Hashtable();
hash.Add("1", "AB");
hash.Add("2", "BC");
hash.Add("3", "DE");
hash.Add("4", "EF");
hash.Add("5", "GH");
hash.Add("6", "IJ");
hash.Add("7", "KL");
hash.Add("8", "MN");
hash.Add("9", "OP");
hash.Add("10", "QR");
Console.WriteLine("Value at key 3 = "+hash["3"]);
Console.WriteLine("Does Hashtable contains the key 12 = "+hash.Contains("12"));
Console.WriteLine("Does Hashtable contains the key 5 = "+hash.Contains("5"));
Console.WriteLine("Does Hashtable contains the key 15 = "+hash.Contains("15"));
}
}
The output of the above code is −
Value at key 3 = DE Does Hashtable contains the key 12 = False Does Hashtable contains the key 5 = True Does Hashtable contains the key 15 = False
Key Points
-
The
Contains()method only checks for keys, not values -
Key comparison is case-sensitive for string keys
-
The method returns
falseif the key isnulland the Hashtable doesn't allow null keys -
Use
ContainsValue()method if you need to check for values instead of keys
Conclusion
The Contains() method provides an efficient way to check if a specific key exists in a Hashtable before performing operations like retrieval or modification. This helps prevent exceptions and ensures your code handles missing keys gracefully.
