Single.GetHashCode() Method in C# with Examples


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

Syntax

The syntax is as follows −

public override int GetHashCode ();

Example

Let us now see an example −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      float f1 = 40.2f;
      object f2 = 50.5f;
      Console.WriteLine("Value1 = "+f1);
      Console.WriteLine("HashCode of Value1 = "+f1.GetHashCode());
      Console.WriteLine("Value2 = "+f2);
      Console.WriteLine("HashCode of Value2 = "+f2.GetHashCode());
      Console.WriteLine("Are both the values equal? = "+f1.Equals(f2));
   }
}

Output

This will produce the following output −

Value1 = 40.2
HashCode of Value1 = 1109445837
Value2 = 50.5
HashCode of Value2 = 1112145920
Are both the values equal? = False

Example

Let us now see another example −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      float f1 = 15.3f;
      float f2 = 35.9f;
      Console.WriteLine("Value1 = "+f1);
      Console.WriteLine("Hashcode for Value1 = "+f1.GetHashCode());
      Console.WriteLine("Value2 = "+f2);
      Console.WriteLine("Hashcode for Value2 = "+f2.GetHashCode());
      Console.WriteLine("Is f1 and f2 equal? = "+f1.CompareTo(f2));
   }
}

Output

This will produce the following output −

Value1 = 15.3
Hashcode for Value1 = 1098173645
Value2 = 35.9
Hashcode for Value2 = 1108318618
Is f1 and f2 equal? = -1
raja
Published on 04-Dec-2019 12:03:42
Advertisements