Get the HashCode for the current UInt64 instance in C#

The GetHashCode() method in C# returns a hash code for the current UInt64 instance. Hash codes are primarily used in hash tables and dictionaries to quickly locate objects. For UInt64 values, the hash code is typically derived from the numeric value itself.

Syntax

Following is the syntax for getting the hash code of a UInt64 instance −

public override int GetHashCode()

Return Value

The method returns a 32-bit signed integer hash code representing the current UInt64 value.

Using GetHashCode() with Small Values

For smaller UInt64 values that fit within the range of a 32-bit integer, the hash code often equals the value itself −

using System;

public class Demo {
   public static void Main() {
      ulong val1 = 8768768;
      ulong val2 = UInt64.MinValue;
      Console.WriteLine("HashCode for val1 = " + val1.GetHashCode());
      Console.WriteLine("HashCode for val2 = " + val2.GetHashCode());
      Console.WriteLine("val1 value = " + val1);
      Console.WriteLine("val2 value = " + val2);
   }
}

The output of the above code is −

HashCode for val1 = 8768768
HashCode for val2 = 0
val1 value = 8768768
val2 value = 0

Using GetHashCode() with Large Values

For larger UInt64 values that exceed the 32-bit integer range, the hash code is computed differently and may not equal the original value −

using System;

public class Demo {
   public static void Main() {
      ulong val1 = 89879878;
      ulong val2 = UInt64.MaxValue;
      Console.WriteLine("HashCode for val1 = " + val1.GetHashCode());
      Console.WriteLine("HashCode for val2 = " + val2.GetHashCode());
      Console.WriteLine("val1 value = " + val1);
      Console.WriteLine("val2 value = " + val2);
   }
}

The output of the above code is −

HashCode for val1 = 89879878
HashCode for val2 = -1
val1 value = 89879878
val2 value = 18446744073709551615

Hash Code Properties

The GetHashCode() method for UInt64 follows these important principles −

using System;

public class Demo {
   public static void Main() {
      ulong val1 = 12345;
      ulong val2 = 12345;
      ulong val3 = 54321;
      
      Console.WriteLine("val1 HashCode = " + val1.GetHashCode());
      Console.WriteLine("val2 HashCode = " + val2.GetHashCode());
      Console.WriteLine("val3 HashCode = " + val3.GetHashCode());
      Console.WriteLine("val1 == val2: " + (val1 == val2));
      Console.WriteLine("HashCodes equal: " + (val1.GetHashCode() == val2.GetHashCode()));
   }
}

The output of the above code is −

val1 HashCode = 12345
val2 HashCode = 12345
val3 HashCode = 54321
val1 == val2: True
HashCodes equal: True

Key Rules

  • If two UInt64 values are equal, their hash codes must be equal.

  • The hash code should remain consistent during the object's lifetime.

  • Hash codes are used internally by collections like Dictionary and HashSet.

  • For very large UInt64 values, the hash code is computed using internal algorithms.

Conclusion

The GetHashCode() method for UInt64 returns a 32-bit integer hash code used primarily in hash-based collections. Equal UInt64 values always produce equal hash codes, making them suitable for efficient storage and retrieval in dictionaries and hash sets.

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

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements