How to get hash code for the specified key of a Hashtable in C#?

To get the hash code for a specified key in a Hashtable, you need to use the GetHash() method. This method is protected in the base Hashtable class, so you must create a derived class to access it. The hash code is an integer value used internally by the hashtable to determine where to store the key-value pair.

Syntax

Following is the syntax to access the protected GetHash() method −

public class CustomHashtable : Hashtable {
   public int GetHashCodeForKey(object key) {
      return GetHash(key);
   }
}

How It Works

The GetHash() method computes the hash code for a given key using the key's GetHashCode() method. This hash code determines the internal bucket where the key-value pair will be stored in the hashtable for efficient retrieval.

Hashtable Hash Code Process Key "D" GetHash() Hash Code -842352676 Hash code determines internal bucket location for efficient storage and retrieval

Using GetHash() with String Keys

Example

using System;
using System.Collections;

public class HashCode : Hashtable {
   public static void Main(string[] args) {
      HashCode hash = new HashCode();
      hash.Add("A", "Jacob");
      hash.Add("B", "Mark");
      hash.Add("C", "Tom");
      hash.Add("D", "Nathan");
      hash.Add("E", "Tim");
      hash.Add("F", "John");
      hash.Add("G", "Gary");
      
      Console.WriteLine("Key and Value pairs...");
      foreach(DictionaryEntry entry in hash) {
         Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
      }
      Console.WriteLine("HashCode for key D = " + (hash.GetHash("D")));
   }
}

The output of the above code is −

Key and Value pairs...
G and Gary
A and Jacob
B and Mark
C and Tom
D and Nathan
E and Tim
F and John
HashCode for key D = -842352676

Using GetHash() with Character Keys

Example

using System;
using System.Collections;

public class HashCode : Hashtable {
   public static void Main(string[] args) {
      HashCode hash = new HashCode();
      hash.Add('1', "One");
      hash.Add('2', "Two");
      hash.Add('3', "Three");
      hash.Add('4', "Four");
      
      Console.WriteLine("Key and Value pairs...");
      foreach(DictionaryEntry entry in hash) {
         Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
      }
      Console.WriteLine("HashCode for key 1 = " + (hash.GetHash('1')));
      Console.WriteLine("HashCode for key 2 = " + (hash.GetHash('2')));
      Console.WriteLine("HashCode for key 3 = " + (hash.GetHash('3')));
      Console.WriteLine("HashCode for key 4 = " + (hash.GetHash('4')));
   }
}

The output of the above code is −

Key and Value pairs...
3 and Three
2 and Two
4 and Four
1 and One
HashCode for key 1 = 3211313
HashCode for key 2 = 3276850
HashCode for key 3 = 3342387
HashCode for key 4 = 3407924

Key Points

  • The GetHash() method is protected in the Hashtable class, requiring inheritance to access it.

  • Hash codes are integers that can be positive or negative.

  • The same key will always produce the same hash code within the same application run.

  • Hash codes are used internally for efficient storage and retrieval of key-value pairs.

Conclusion

The GetHash() method allows you to retrieve the hash code for any key in a Hashtable by creating a derived class. This hash code is an integer value used internally by the hashtable to efficiently organize and locate key-value pairs within its internal data structure.

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

331 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements