C# | How to get hash code for the specified key of a Hashtable Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 illustrate the use of above-discussed method: Example 1: csharp // C# Program to illustrate the // Hashtable.GetHash(Object) method using System; using System.Collections; // Inheriting Hashtable as // Hashtable.GetHash(Object) // method is protected method class HashCode : Hashtable { // Main Method static void Main(string[] args) { // creating object for HashCode as // to access protected methods we // have to create object for the // derived class HashCode h = new HashCode(); // Add Elements into Hashtable h.Add("1001", "Parek Shetty"); h.Add("1002", "Deshmuk Narayan"); h.Add("1003", "Ratan Kaalikaran"); ICollection Key = h.Keys; foreach(string val in Key) { // printing Hashtable Console.Write(val + " : " + h[val]); Console.Write("\n"); // printing hashcode with keys int hcode = h.GetHash(val); Console.Write(val + " : " + hcode); Console.Write("\n"); } } } Output: 1002 : Deshmuk Narayan 1002 : 985757554 1001 : Parek Shetty 1001 : -1708895167 1003 : Ratan Kaalikaran 1003 : -1892225314 Example 2: csharp // C# Program to illustrate the // Hashtable.GetHash(Object) method using System; using System.Collections; class HashCode : Hashtable { // Main Method static void Main(string[] args) { HashCode h = new HashCode(); // Adding elements h.Add('A', "Pritam Devadhya"); h.Add('B', "Arjun Balachi"); h.Add('C', "Timanad Panigrahi"); ICollection Key = h.Keys; int hcode = h.GetHash('C'); Console.Write("HashCode: " + hcode); } } Output: HashCode: 4390979 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable.gethash?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check whether a Hashtable contains a specific key or not K Krishna_Sai_Ketha Follow Improve Article Tags : C# 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 Hashtable contains a specific key or not 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 2 min read Getting the Hash Code of the Specified Range in C# The Range Structure is introduced in C# 8.0. It represents a range that has a start and end indexes. You are allowed to get the hash code of the specified range with the help of the GetHashCode() Method provided by the Range struct. This method returns the hash code of the specified instance. Syntax 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# | 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. NotSup 3 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 virt 2 min read Like