Get the hash code for the current Int64 instance in C#

The GetHashCode() method in C# returns a hash code for the current Int64 instance. This method is inherited from the Object class and is commonly used in hash-based collections like Dictionary and HashSet to efficiently store and retrieve values.

Hash codes are 32-bit integers that provide a quick way to compare objects for equality. Two equal Int64 values will always have the same hash code, but different values may occasionally produce the same hash code (called a collision).

Syntax

Following is the syntax for getting the hash code of an Int64 value −

public override int GetHashCode();

Return Value

The method returns a 32-bit signed integer hash code for the current Int64 instance.

Using GetHashCode() with Regular Int64 Values

The following example demonstrates how to get hash codes for different Int64 values −

using System;

public class Demo {
   public static void Main() {
      long val1 = 8768768768;
      long val2 = 7889765555;
      Console.WriteLine("Value1 = " + val1);
      Console.WriteLine("Value2 = " + val2);
      Console.WriteLine("Are they equal? = " + val1.Equals(val2));
      Console.WriteLine("Value1 (HashCode) = " + val1.GetHashCode());
      Console.WriteLine("Value2 (HashCode) = " + val2.GetHashCode());
   }
}

The output of the above code is −

Value1 = 8768768768
Value2 = 7889765555
Are they equal? = False
Value1 (HashCode) = 178834178
Value2 (HashCode) = -700169038

Using GetHashCode() with Extreme Int64 Values

This example shows hash codes for boundary values of Int64

using System;

public class Demo {
   public static void Main() {
      long val1 = 0;
      long val2 = Int64.MaxValue;
      long val3 = Int64.MinValue;
      Console.WriteLine("Value1 = " + val1);
      Console.WriteLine("Value2 = " + val2);
      Console.WriteLine("Value3 = " + val3);
      Console.WriteLine("Value1 (HashCode) = " + val1.GetHashCode());
      Console.WriteLine("Value2 (HashCode) = " + val2.GetHashCode());
      Console.WriteLine("Value3 (HashCode) = " + val3.GetHashCode());
   }
}

The output of the above code is −

Value1 = 0
Value2 = 9223372036854775807
Value3 = -9223372036854775808
Value1 (HashCode) = 0
Value2 (HashCode) = -2147483648
Value3 (HashCode) = -2147483648

Hash Code Consistency

Equal Int64 values always produce identical hash codes, as demonstrated below −

using System;

public class Demo {
   public static void Main() {
      long val1 = 12345;
      long val2 = 12345;
      long val3 = 54321;
      
      Console.WriteLine("val1 = " + val1 + ", HashCode = " + val1.GetHashCode());
      Console.WriteLine("val2 = " + val2 + ", HashCode = " + val2.GetHashCode());
      Console.WriteLine("val3 = " + val3 + ", HashCode = " + val3.GetHashCode());
      Console.WriteLine("val1 equals val2: " + val1.Equals(val2));
      Console.WriteLine("Hash codes equal: " + (val1.GetHashCode() == val2.GetHashCode()));
   }
}

The output of the above code is −

val1 = 12345, HashCode = 12345
val2 = 12345, HashCode = 12345
val3 = 54321, HashCode = 54321
val1 equals val2: True
Hash codes equal: True

Common Use Cases

  • Used internally by hash-based collections like Dictionary<TKey, TValue> and HashSet<T>

  • Implementing custom equality comparisons in user-defined classes

  • Optimizing object comparisons by first checking hash codes before detailed equality checks

Conclusion

The GetHashCode() method for Int64 returns a 32-bit integer hash code that uniquely represents the value. Equal Int64 values always produce the same hash code, making this method essential for hash-based collections and efficient equality comparisons.

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

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements