Int16.GetHashCode() Method in C# with Examples

CsharpServer Side ProgrammingProgramming

The Int16.GetHashCode() method in C# is used to return the hash code for the current instance.

Syntax

Following is the syntax −

public override int GetHashCode ();

Example

Let us now see an example to implement the Int16.GetHashCode() method −

using System;
public class Demo {
   public static void Main(){
      short val1 = 20;
      short val2 = 25;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("HashCode for value1 = "+val1.GetHashCode());
      Console.WriteLine("HashCode for value2 = "+val2.GetHashCode());
      Console.WriteLine("Are they equal? = "+(val1.Equals(val2)));
   }
}

Output

This will produce the following output −

Value1 = 20
Value2 = 25
HashCode for value1 = 1310740
HashCode for value2 = 1638425
Are they equal? = False

Example

Let us now see another example to implement the Int16.GetHashCode() method −

using System;
public class Demo {
   public static void Main(){
      short val1 = 0;
      short val2 = Int16.MaxValue;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("HashCode for value1 = "+val1.GetHashCode());
      Console.WriteLine("HashCode for value2 = "+val2.GetHashCode());
      Console.WriteLine("Are they equal? = "+(val1.Equals(val2)));
   }
}

Output

This will produce the following output −

Value1 = 0
Value2 = 32767
HashCode for value1 = 0
HashCode for value2 = 2147450879
Are they equal? = False
raja
Published on 11-Nov-2019 06:41:31
Advertisements