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