C# 中的 DateTimeOffset.GetHashCode() 方法
C# 中的 DateTimeOffset.GetHashCode 方法用于返回当前 DateTimeOffset 对象的哈希值。此方法返回一个 32 位有符号整数哈希值。
语法
语法如下 −
public override int GetHashCode ();
示例
下面我们看一个实现 DateTimeOffset.GetHashCode() 方法的示例 −
using System; public class Demo { public static void Main() { DateTimeOffset dateTimeOffset1 = new DateTimeOffset(2019, 09, 10, 6, 20, 10, new TimeSpan(-5, 0, 0)); DateTimeOffset dateTimeOffset2 = new DateTimeOffset(2019, 11, 12, 8, 20, 10, new TimeSpan(-5, 0, 0)); Console.WriteLine("DateTimeOffset1 = {0}", dateTimeOffset1); int hash1 = dateTimeOffset1.GetHashCode(); Console.WriteLine("DateTimeOffset1 HashCode = {0}", hash1); int hash2 = dateTimeOffset2.GetHashCode(); Console.WriteLine("DateTimeOffset2 HashCode = {0}", hash2); Console.WriteLine("DateTimeOffset2 = {0}", dateTimeOffset2); int res = DateTimeOffset.Compare(dateTimeOffset1, dateTimeOffset2); // returns <0 since DateTimeOffset1 is earlier than DateTimeOffset2 Console.WriteLine(res); } }
输出
这会产生以下输出 −
DateTimeOffset1 = 9/10/2019 6:20:10 AM -05:00 DateTimeOffset1 HashCode = -543087392 DateTimeOffset2 HashCode = 64112243 DateTimeOffset2 = 11/12/2019 8:20:10 AM -05:00 -1
示例
现在让我们再看一个实现 DateTimeOffset.GetHashCode() 的示例 −
using System; public class Demo { public static void Main() { DateTimeOffset dateTimeOffset1 = new DateTimeOffset(2019, 11, 10, 6, 20, 10, new TimeSpan(-5, 0, 0)); Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset1); int hash = dateTimeOffset1.GetHashCode(); Console.WriteLine("DateTimeOffset HashCode = {0}", hash); } }
输出
这会产生以下输出 −
DateTimeOffset = 11/10/2019 6:20:10 AM -05:00 DateTimeOffset HashCode = -92293937
广告