Open In App

C# | Check whether a Hashtable contains a specific key or not

Last Updated : 01 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Hashtable.Contains(Object) Method is used to check whether the Hashtable contains a specific key or not.

Syntax:

public virtual bool Contains (object key);

Here, key is the Key of Object type which is to be located in the Hashtable.

Return Value: This method returns true if the Hashtable contains an element with the specified key otherwise returns false.

Exception: This method will give ArgumentNullException if the key is null.

Note:

  • Hashtable.ContainsKey(Object) Method is also used to check whether the Hashtable contains a specific key or not. This method behaves same as Contains() method.
  • Contains method implements IDictionary.Contains. It behaves exactly as ContainsKey and this method is an O(1) operation.

Below programs illustrate the use of above-discussed method:

Example 1:




// C# code to check whether the Hashtable
// contains a specific key or not
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Hashtable
        Hashtable myTable = new Hashtable();
  
        // Adding elements in Hashtable
        myTable.Add("g", "geeks");
        myTable.Add("c", "c++");
        myTable.Add("d", "data structures");
        myTable.Add("q", "quiz");
  
        // Checking if Hashtable contains
        // the key "Brazil"
        Console.WriteLine(myTable.Contains("d"));
    }
}


Output:

True

Example 2:




// C# code to check whether the Hashtable
// contains a specific key or not
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Hashtable
        Hashtable myTable = new Hashtable();
  
        // Adding elements in Hashtable
        myTable.Add("1", "C");
        myTable.Add("2", "C++");
        myTable.Add("3", "Java");
        myTable.Add("4", "Python");
  
        // Checking if Hashtable contains
        // the key null. It will give exception
        // ArgumentNullException
        Console.WriteLine(myTable.Contains(null));
    }
}


Runtime Error:

Unhandled Exception:
System.ArgumentNullException: Key cannot be null.
Parameter name: key

Reference:



Similar Reads

C# | Check if the Hashtable contains a specific Key
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsKey(Object) Method is used to check whether the Hashtable contains a specific key or not. Syntax: public virtual bool ContainsKey(object key); Parameter: ke
2 min read
C# | Check if the Hashtable contains a specific Value
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsValue(Object) Method is used to check whether the Hashtable contains a specific value or not. Syntax: public virtual bool ContainsValue(object value); Param
2 min read
C# | Check if a Hashtable is equal to another Hashtable
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Syntax: myTable1.Equals(myTable2) Here, myTable1 and myTable2 are the two Hashtables which is to be checked. Below given are some examples to understand the implementation in
2 min read
C# | Check whether a SortedList object contains a specific key
SortedList.Contains(Object) Method is used to check whether a SortedList object contains a specific key. Syntax: public virtual bool Contains (object key); Here, key is the Key which is to be located in the SortedList object. Return Value: This method returns the true if the SortedList object contains an element with the specified key otherwise, it
2 min read
C# | Check if the StringDictionary contains a specific key
StringDictionary.ContainsKey(String) method is used to check whether the StringDictionary contains a specific key or not. Syntax: public virtual bool ContainsKey (string key); Here, key is the key to locate in the StringDictionary. Return Value: This method returns true if the StringDictionary contains an entry with the specified key, otherwise it
2 min read
C# | Check if ListDictionary contains a specific key
ListDictionary.Contains(Object) method is used to check whether the ListDictionary contains a specific key or not. Syntax: public bool Contains (object key); Here, key is the key to locate in the ListDictionary. Return Value: The method returns true if the ListDictionary contains an entry with the specified key, otherwise it returns false. Exceptio
2 min read
C# | Check if OrderedDictionary collection contains a specific key
OrderedDictionary.Contains(Object) method is used to check whether the OrderedDictionary collection contains a specific key or not. Syntax: public bool Contains (object key); Here, key is the key to locate in the OrderedDictionary collection. Return Value: This method returns True if the OrderedDictionary collection contains an element with the spe
2 min read
C# | Remove the element with the specified key from the Hashtable
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Remove(Object) Method is used to remove the element with the specified key from the Hashtable. Syntax: public virtual void Remove (object key); Parameter: key: It i
2 min read
C# | Count the number of key/value pairs in the Hashtable
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Count Property is used to get the total number of the key/value pairs contained in the Hashtable. Syntax: myTable.Count Here, myTable is the name of the Hashtable.
2 min read
C# | Get or Set the value associated with specified key in Hashtable
Hashtable.Item[Object] Property is used to get or set the value associated with the specified key in the Hashtable. Syntax: public virtual object this[object key] { get; set; } Here, key is key of object type whose value is to get or set. Exceptions: ArgumentNullException: If the key is null. NotSupportedException: If the property is set and the Ha
3 min read