📜  C#|如何获得元组的第二元素?

📅  最后修改于: 2021-05-29 20:41:20             🧑  作者: Mango

元组是一种数据结构,它为您提供最简单的方式来表示一个数据集,该数据集具有多个可能/可能彼此不相关的值。 Item2属性用于获取给定元组的第二个元素。它不适用于1-Tuple,也适用于所有其他剩余的Tuple。

句法:

public T2 Item2 { get; }

在此, T2是当前Tuple <>对象的第二个组件的值。该元组<>可以是2元组,也可以是3元组,或4元组,或5元组,或6元组,或7元组,或8元组。

示例:在下面的代码中,您可以看到我们正在访问每个元组的第二个元素。

// C# program to illustrate how to get 
// the second element of the tuple
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Taking 2-tuple
        var st2 = Tuple.Create("Sohan", 20);
        Console.WriteLine("Student-2 Age: " + st2.Item2);
  
        // Taking 3-tuple
        var st3 = Tuple.Create("Soniya", 30, "CSE");
        Console.WriteLine("Student-3 Age: " + st3.Item2);
  
        // Taking 4-tuple
        var st4 = Tuple.Create("Rohan", 29, "CSE", 2015);
        Console.WriteLine("Student-4 Age: " + st4.Item2);
  
        // Taking 5-tuple
        var st5 = Tuple.Create("Siya", 22, "CSE", 2017,
                                           "20-Mar-1993");
  
        Console.WriteLine("Student-5 Age: " + st5.Item2);
  
        // Taking 6-tuple
        var st6 = Tuple.Create("Riya", 24, "CSE", 2015,
                               "30-May-2015", 230134832);
  
        Console.WriteLine("Student-6 Age: " + st6.Item2);
  
        // Taking 7-tuple
        var st7 = Tuple.Create("Rohit", 21, "CSE", 2017, 
                        "21-Apr-1998", 384749829, 20000);
  
        Console.WriteLine("Student-7 Age: " + st7.Item2);
  
        // Taking 8-tuple
        var st8 = Tuple.Create("Manita", 24, "CSE", 2016, 
                   "03-Aug-1991", 235678909, 34000, "C#");
  
        Console.WriteLine("Student-8 Age: " + st8.Item2);
    }
}
输出:
Student-2 Age: 20
Student-3 Age: 30
Student-4 Age: 29
Student-5 Age: 22
Student-6 Age: 24
Student-7 Age: 21
Student-8 Age: 24