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

📅  最后修改于: 2021-05-29 18:32:35             🧑  作者: Mango

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

句法:

public T4 Item4 { get; }

在此, T4是当前Tuple <>对象的第四部分的值。该元组<>可以是4元组,也可以是5元组,或6元组,或7元组,或8元组。

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

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