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


To get the hash code for the current Int64 instance, the code is as follows −

Example

 Live Demo

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());
   }
}

Output

This will produce the following output −

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

Example

Let us see another example −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      long val1 = 0;
      long val2 = Int64.MaxValue;
      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());
   }
}

Output

This will produce the following output −

Value1 = 0
Value2 = 9223372036854775807
Are they equal? = False
Value1 (HashCode) = 0
Value2 (HashCode) = -2147483648

Updated on: 05-Dec-2019

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements