Get the hash code for this instance in C#

To get the hash code for this instance in C#, you use the GetHashCode() method. This method returns a hash code for the current object, which is useful for hash-based operations and collections like HashSet and Dictionary.

Syntax

Following is the syntax for getting the hash code of an instance −

public virtual int GetHashCode()

Return Value

The GetHashCode() method returns a 32-bit signed integer that serves as the hash code for this instance. Objects that are equal should return the same hash code, though the reverse is not necessarily true.

Using GetHashCode() with Type Objects

Example

using System;

public class Demo {
   public static void Main() {
      string[] arr = {"tom", "amit", "kevin", "katie"};
      Type t1 = arr.GetType();
      Type t2 = t1.GetElementType();
      Console.WriteLine("Type = " + t2.ToString());
      Console.WriteLine("Hash Code = " + t1.GetHashCode());
   }
}

The output of the above code is −

Type = System.String
Hash Code = 9144789

Using GetHashCode() with Enums

Example

using System;

public class Demo {
   enum Vehicle {Car, Bus, Bike, Airplane}
   
   public static void Main() {
      try {
         Vehicle v = Vehicle.Bike;
         Type type = v.GetType();
         Console.WriteLine("Hash code = " + type.GetHashCode());
         string[] str = type.GetEnumNames();
         Console.WriteLine("GetEnumName() to return the constant name = " + str);
         Type type2 = type.GetEnumUnderlyingType();
         Console.Write("Enum Underlying type = " + type2);
         Array arrObj = type.GetEnumValues();
         Console.Write("Values = " + arrObj);
         Console.WriteLine("\nListing constants ..");
         for (int i = 0; i < str.Length; i++)
            Console.Write("{0} ", str[i]);
      }
      catch (ArgumentException e) {
         Console.WriteLine("Not an enum!");
         Console.Write("{0}", e.GetType(), e.Message);
      }
   }
}

The output of the above code is −

Hash code = 44757566
GetEnumName() to return the constant name = System.String[]
Enum Underlying type = System.Int32Values = Demo+Vehicle[]
Listing constants ..
Car Bus Bike Airplane

Using GetHashCode() with Custom Objects

Example

using System;

public class Person {
   public string Name { get; set; }
   public int Age { get; set; }
   
   public Person(string name, int age) {
      Name = name;
      Age = age;
   }
   
   public override int GetHashCode() {
      return Name.GetHashCode() ^ Age.GetHashCode();
   }
}

public class Demo {
   public static void Main() {
      Person p1 = new Person("John", 25);
      Person p2 = new Person("Jane", 30);
      
      Console.WriteLine("Person 1 Hash Code: " + p1.GetHashCode());
      Console.WriteLine("Person 2 Hash Code: " + p2.GetHashCode());
      Console.WriteLine("String Hash Code: " + "Hello".GetHashCode());
      Console.WriteLine("Integer Hash Code: " + 42.GetHashCode());
   }
}

The output of the above code is −

Person 1 Hash Code: 372029373
Person 2 Hash Code: 372029405
String Hash Code: -327378614
Integer Hash Code: 42

Key Rules

  • Objects that are equal must have the same hash code.

  • Hash codes should be distributed evenly to avoid clustering in hash tables.

  • Hash codes can be negative since they are signed integers.

  • The same object returns the same hash code during its lifetime unless modified.

Conclusion

The GetHashCode() method provides a hash code for any object instance in C#. It's essential for hash-based collections and can be overridden in custom classes to provide meaningful hash codes based on object properties.

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

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements