如何获取 C# 中元组的 HashCode?


要获取元组的 HashCode,代码如下 −

举例

 动态演示

using System;
public class Demo {
   public static void Main(String[] args){
      var tuple1 = Tuple.Create(150, 400, 500, 700, 100, 1200, 1500, Tuple.Create(50, 100));
      var tuple2 = Tuple.Create(150, 400, 500, 700, 100, 1200, 1500, Tuple.Create(100, 200));
      Console.WriteLine("Is Tuple1 equal to Tuple2? = "+tuple1.Equals(tuple2));
      Console.WriteLine("HashCode of Tuple1 = "+tuple1.GetHashCode());
      Console.WriteLine("HashCode of Tuple2 = "+tuple2.GetHashCode());
   }
}

结果

这将产生以下结果 −

Is Tuple1 equal to Tuple2? = False HashCode of Tuple1 = 4621968
HashCode of Tuple2 = 4623530

举例

我们来看另一个例子 −

 动态演示

using System;
public class Demo {
   public static void Main(String[] args){
      var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500);
      var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500);
      Console.WriteLine("Is Tuple1 equal to Tuple2? = "+tuple1.Equals(tuple2));
      Console.WriteLine("HashCode of Tuple1 = "+tuple1.GetHashCode());
      Console.WriteLine("HashCode of Tuple2 = "+tuple2.GetHashCode());
   }
}

结果

这将产生以下结果 −

Is Tuple1 equal to Tuple2? = True HashCode of Tuple1 = 3231587
HashCode of Tuple2 = 3231587

更新于: 11-Dec-2019

134 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始
广告