此方法用于返回此实例的哈希码。
Syntax: public override int GetHashCode ();
Return Value: This method returns a 32-bit signed integer hash code.
下面的程序说明了DateTime.GetHashCode()方法的用法:
范例1:
// C# program to demonstrate the
// DateTime.GetHashCode()
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
// creating object of DateTime
DateTime date = new DateTime(2010, 1,
1, 4, 0, 15);
// getting hashcode from DateTime
// using GetHashCode() method;
int value = date.GetHashCode();
// Display the hashcode
Console.WriteLine("hashcode is {0}", value);
}
}
输出:
hashcode is 103491886
范例2:
// C# program to demonstrate the
// DateTime.GetHashCode()
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
// calling check() method
check(new DateTime(2010, 1,
3, 4, 0, 15));
check(new DateTime(2010, 1,
5, 4, 0, 15));
}
public static void check(DateTime date)
{
// getting HashCode from DateTime
// using GetHashCode() method;
int value = date.GetHashCode();
// Display the ShortTime
Console.WriteLine("HashCode of {0} is {1}",
date, value);
}
}
输出:
HashCode of 01/03/2010 04:00:15 is 1802939328
HashCode of 01/05/2010 04:00:15 is -1337841070
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.datetime.gethashcode?view=netframework-4.7.2