C# Program to find a key in a Hashtable

A Hashtable in C# is a collection of key-value pairs where each key is unique. To check if a specific key exists in a Hashtable, you can use the Contains() method or ContainsKey() method.

Syntax

Following is the syntax for creating a Hashtable and checking if a key exists −

Hashtable hashtable = new Hashtable();
hashtable.Add(key, value);
bool exists = hashtable.Contains(key);

Alternatively, you can use ContainsKey() method −

bool exists = hashtable.ContainsKey(key);

Using Contains() Method

The Contains() method returns true if the specified key exists in the Hashtable, otherwise false

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        Hashtable h = new Hashtable();
        h.Add(1, "Jack");
        h.Add(2, "Henry");
        h.Add(3, "Ben");
        h.Add(4, "Chris");
        
        Console.WriteLine("Keys and Values list:");
        foreach (var key in h.Keys) {
            Console.WriteLine("Key = {0}, Value = {1}", key, h[key]);
        }
        
        Console.WriteLine("Key 3 exists? " + h.Contains(3));
        Console.WriteLine("Key 5 exists? " + h.Contains(5));
    }
}

The output of the above code is −

Keys and Values list:
Key = 4, Value = Chris
Key = 3, Value = Ben
Key = 2, Value = Henry
Key = 1, Value = Jack
Key 3 exists? True
Key 5 exists? False

Using ContainsKey() Method

The ContainsKey() method provides the same functionality as Contains() but is more explicit about checking keys −

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        Hashtable students = new Hashtable();
        students.Add("ID001", "Alice");
        students.Add("ID002", "Bob");
        students.Add("ID003", "Charlie");
        
        string searchKey = "ID002";
        if (students.ContainsKey(searchKey)) {
            Console.WriteLine("Student found: " + students[searchKey]);
        } else {
            Console.WriteLine("Student with key " + searchKey + " not found");
        }
        
        searchKey = "ID004";
        if (students.ContainsKey(searchKey)) {
            Console.WriteLine("Student found: " + students[searchKey]);
        } else {
            Console.WriteLine("Student with key " + searchKey + " not found");
        }
    }
}

The output of the above code is −

Student found: Bob
Student with key ID004 not found

Checking Multiple Keys

You can check for multiple keys in a loop to find which ones exist in the Hashtable −

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        Hashtable products = new Hashtable();
        products.Add(101, "Laptop");
        products.Add(102, "Mouse");
        products.Add(103, "Keyboard");
        
        int[] keysToFind = {101, 104, 102, 105};
        
        Console.WriteLine("Checking for product keys:");
        foreach (int key in keysToFind) {
            if (products.Contains(key)) {
                Console.WriteLine("Product {0}: {1} - Found", key, products[key]);
            } else {
                Console.WriteLine("Product {0}: Not Found", key);
            }
        }
    }
}

The output of the above code is −

Checking for product keys:
Product 101: Laptop - Found
Product 104: Not Found
Product 102: Mouse - Found
Product 105: Not Found

Conclusion

The Contains() and ContainsKey() methods in C# Hashtable allow you to efficiently check if a specific key exists before performing operations like retrieval or modification. Both methods return a boolean value indicating the presence of the key in the collection.

Updated on: 2026-03-17T07:04:35+05:30

498 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements