如何在 C# 中获取元组的第一个元素?


如需获取元组的第一个元素,代码如下 −

示例

 实时演示

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());
      Console.WriteLine("Tuple1 Item 5th = "+tuple1.Item5);
      Console.WriteLine("Tuple2 Item 5th = "+tuple2.Item5);
      Console.WriteLine("Tuple1 Item 1st = "+tuple1.Item1);
      Console.WriteLine("Tuple2 Item 1st = "+tuple2.Item1);
   }
}

输出

这将产生以下输出 −

Is Tuple1 equal to Tuple2? = True HashCode of Tuple1 = 3231587
HashCode of Tuple2 = 3231587
Tuple1 Item 5th = 100
Tuple2 Item 5th = 100
Tuple1 Item 1st = 75
Tuple2 Item 1st = 75

示例

让我们看另一个示例 −

 实时演示

using System;
public class Demo {
   public static void Main(String[] args){
      var tuple = Tuple.Create(1200, 1500, 2200, 2700, 3100, 3500, 4500, 5500);
      Console.WriteLine("HashCode of Tuple = "+tuple.GetHashCode());
      Console.WriteLine("Tuple Item 1st = "+tuple.Item1);
      Console.WriteLine("Tuple Item 2nd = "+tuple.Item2);
      Console.WriteLine("Tuple Item 3rd = "+tuple.Item3);
      Console.WriteLine("Tuple Item 4th = "+tuple.Item4);
      Console.WriteLine("Tuple Item 5th = "+tuple.Item5);
      Console.WriteLine("Tuple Item 6th = "+tuple.Item6);
      Console.WriteLine("Tuple Item 7th = "+tuple.Item7);
   }
}

输出

这将产生以下输出 −

HashCode of Tuple = 49989024
Tuple Item 1st = 1200
Tuple Item 2nd = 1500
Tuple Item 3rd = 2200
Tuple Item 4th = 2700
Tuple Item 5th = 3100
Tuple Item 6th = 3500
Tuple Item 7th = 4500

更新日期:2019 年 12 月 6 日

727 次浏览

开启你的 职业

通过完成课程获得认证

开始
广告