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.

How to Check if Value Exists in Hashtable?

To check if the value exists in a hashtable, we can make use of the “containsValue” method provided by the Hashtable class. This method returns a Boolean value indicating whether or not the specified value is present in the hashtable.

Let’s have a look at the method first before proceeding with programming examples.

ContainsValue Method

Syntax − public virtual bool ContainsValue (object value);

Description − used to find if the Hashtable contains a specified value.

Parameters − value(object) to be located in the hashtable. Can be a null value.

Return Value  Boolean: true=> hashtable contains an element with the specified value.

False=> hashtable does not contain an element with the specified value.

Namespace − System.Collections

Let’s now see few programming examples where we check if the specified value is present in the hashtable or not.

Example

The first program to check if the value is present in the hashtable is given below.

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");
      
      // use ContainsValue method to check if the HashTable contains the 
      //required Value or not.
      if (langCodes.ContainsValue("CSharp"))
         Console.WriteLine("langCodes hashtable contain the Value = CSharp");
      else
         Console.WriteLine("langCodes hashtable doesn't contain the Value = CSharp");
   }
}

The above program declares a langCodes hashtable containing language code and language names as keys and values respectively. Next, we have an “if” construct that checks if the value “CSharp” is present in the hashtable. If it is present, it will display the message accordingly.

Output

The output of the program is shown below.

langCodes hashtable contain the Value = CSharp

Since the value = CSharp is present in the hashtable, the program gives the above message.

Now change the argument of the ContainsValue method to “C#” i.e. the key instead of value.

if (langCodes.ContainsValue("C#"))

Now execute the above program with this change.

Output

In this case, since the value “C#” is not present in the hashtable, the program will return the appropriate message. So we’ll get −

langCodes hashtable doesn't contain the Value = CSharp

Example

Now let’s look at the following 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");
      // use ContainsValue method to check if the HashTable contains the
      //required Value or not.
      if (NumberNames.ContainsValue("Two"))
         Console.WriteLine("NumberNames hashtable contain the Value = Two");
      else
         Console.WriteLine("NumberNames hashtable doesn't contain the Value = Two");
      if (NumberNames.ContainsValue("Five"))
         Console.WriteLine("NumberNames hashtable contain the Value = Five");
      else
         Console.WriteLine("NumberNames hashtable doesn't contain the Value = Five");
   }
}

This program has a “NumberNames” table with numbers as keys and their corresponding names as values. Here, we first check if the hashtable contains value = Two using the “containsKey()” method. Next, we check for the value = “Five” using the containsKey() method. 

Output

The output for the program is shown below.

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

As we can see from the hashtable defined in the program, it does not contain value = Two but contains value = Five. So the program gives the messages appropriately.

Conclusion

Thus using the “containsKey()” method of the Hashtable class in C#, we can determine if an element with a specific value is present in the hashtable or not. Depending on whether the value is present or not, we can output the appropriate results, or in the case of a complex program, proceed with the appropriate code.

The method “containsKey()” thus becomes useful when we have to check for the presence of a specified value in the hashtable and then take the appropriate course of action.

Updated on: 22-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements