C# Program to Check if Value exists in Hashtable

The hashtable is an organized collection of key-value pairs wherein the keys are arranged as per the hash code of the key calculated using the hash function. While the keys should be non-null and unique in a hashtable, the values can be null and duplicate.

The elements in the hashtable are accessed using the keys. In C#, the class Hashtable represents the hashtable collection. This class provides various properties and methods using which we can perform operations and access data in the hashtable.

In this article, we will see how we can determine if a particular value is present in the hashtable using the ContainsValue method.

Syntax

Following is the syntax for the ContainsValue method

public virtual bool ContainsValue(object value);

Parameters

value The object to be located in the hashtable. Can be a null value.

Return Value

Returns true if the hashtable contains an element with the specified value; otherwise, false.

ContainsValue Method Operation Hashtable "C#" ? "CSharp" "Java" ? "Java" 1 ? "One" ContainsValue("CSharp") ? true ContainsValue("Python") ? false

Using ContainsValue to Check String Values

Example

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable langCodes = new Hashtable();
      
      // Add elements to the Hashtable
      langCodes.Add("C++", "CPlusPlus");
      langCodes.Add("C#", "CSharp");
      langCodes.Add("Java", "Java");
      langCodes.Add("PL", "Perl");
      
      // Check if the hashtable contains the value "CSharp"
      if (langCodes.ContainsValue("CSharp"))
         Console.WriteLine("langCodes hashtable contains the Value = CSharp");
      else
         Console.WriteLine("langCodes hashtable doesn't contain the Value = CSharp");
      
      // Check if the hashtable contains the value "Python" (not present)
      if (langCodes.ContainsValue("Python"))
         Console.WriteLine("langCodes hashtable contains the Value = Python");
      else
         Console.WriteLine("langCodes hashtable doesn't contain the Value = Python");
   }
}

The output of the above code is

langCodes hashtable contains the Value = CSharp
langCodes hashtable doesn't contain the Value = Python

Using ContainsValue to Check Numeric Values

Example

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable NumberNames = new Hashtable();
      
      // Add elements to the Hashtable
      NumberNames.Add(1, "One");
      NumberNames.Add(3, "Three");
      NumberNames.Add(5, "Five");
      NumberNames.Add(7, "Seven");
      
      // Check if the hashtable contains the value "Two"
      if (NumberNames.ContainsValue("Two"))
         Console.WriteLine("NumberNames hashtable contains the Value = Two");
      else
         Console.WriteLine("NumberNames hashtable doesn't contain the Value = Two");
         
      // Check if the hashtable contains the value "Five"
      if (NumberNames.ContainsValue("Five"))
         Console.WriteLine("NumberNames hashtable contains the Value = Five");
      else
         Console.WriteLine("NumberNames hashtable doesn't contain the Value = Five");
   }
}

The output of the above code is

NumberNames hashtable doesn't contain the Value = Two
NumberNames hashtable contains the Value = Five

Checking for Null Values

The ContainsValue method can also check for null values in the hashtable

Example

using System;
using System.Collections;

class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable items = new Hashtable();
      
      // Add elements including null value
      items.Add("key1", "value1");
      items.Add("key2", null);
      items.Add("key3", "value3");
      
      // Check if the hashtable contains null value
      if (items.ContainsValue(null))
         Console.WriteLine("Hashtable contains a null value");
      else
         Console.WriteLine("Hashtable doesn't contain a null value");
         
      Console.WriteLine("Total items in hashtable: " + items.Count);
   }
}

The output of the above code is

Hashtable contains a null value
Total items in hashtable: 3

Conclusion

The ContainsValue method of the Hashtable class in C# provides an efficient way to determine if a specific value exists in the hashtable. This method is particularly useful when you need to validate data presence before performing operations or making decisions based on hashtable contents.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements