C# | Check whether a Hashtable contains a specific key or not Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like 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: CSharp // 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: CSharp // 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: https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable.contains?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check whether a Hashtable contains a specific key or not K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Hashtable CSharp-Collections-Namespace 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 v 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 contai 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: publ 2 min read C# | Check if a HashSet contains the specified element A HashSet is an unordered collection of the unique elements. It is found in System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. HashSet.C 2 min read C# | How to get hash code for the specified key of a Hashtable Hashtable.GetHash(Object) method is used to get the hashcode of the specified key of a Hashtable object. This method is inherited from the Object Class. Syntax: protected virtual int GetHash(Object Key); Exception: This method will give NullReferenceException if the key is null. Below programs illus 2 min read Like