DateTime.GetHashCode() Method in C#


The DateTime.GetHashCode() method in C# is used to returns the hash code for this instance. This return value is a 32-bit signed integer hash code.

Syntax

Following is the syntax −

public override int GetHashCode ();

Example

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

using System;
public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 11, 10, 7, 20, 45);
      int res = d.GetHashCode();
      Console.WriteLine("HashCode for date {0} = {1}", d, res);
   }
}

Output

This will produce the following output −

HashCode for date 11/10/2019 7:20:45 AM = -2002476754

Example

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

using System;
public class Demo {
   public static void Main() {
      DateTime d = DateTime.Now;
      int res = d.GetHashCode();
      Console.WriteLine("HashCode for date {0} = {1}", d, res);
   }
}

Output

This will produce the following output −

HashCode for date 10/16/2019 7:03:37 AM = -16205546
raja
Published on 08-Nov-2019 10:34:50
Advertisements