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

📅  最后修改于: 2021-05-29 17:45:22             🧑  作者: Mango

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

句法:

public T7 Item7 { get; }

在此, T7是当前Tuple <>对象的第七个分量的值。该Tuple <>可以是7元组,也可以是8元组。

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

// C# program to illustrate how to get 
// the seventh element of the tuple
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
  
        // Taking 7-tuple
        var st7 = Tuple.Create("Rohit", 21, "IT", 2017, 
                        "21-Apr-1994", 384749829, 1);
  
        Console.WriteLine("Student-7 Position.: " + st7.Item7);
  
        // Taking 8-tuple
        var st8 = Tuple.Create("Manita", 24, "CSE", 2013, 
                   "03-Aug-1991", 235678909, 2, "C#");
  
        Console.WriteLine("Student-8 Position: " + st8.Item7);
    }
}
输出:
Student-7 Position.: 1
Student-8 Position: 2

注意:您还可以通过使用GetType()方法或使用Type.GetGenericArguments方法来获取元组的第七个组件的类型。

例子:

// C# program to illustrate how to get the 
// type of the Seventh element of the tuple
using System;
   
class GFG{
       
    // Main method
    static public void Main (){
           
        // Taking 7-tuple
        var stu7 = Tuple.Create("Riya", 24, "CSE", 2015, 102, 230134832, "DSA");
        Console.WriteLine("Student-7 Favourite Subject: "+stu7.Item7);
           
        // Get the type of Item7
        // Using GetType() method
        Console.WriteLine("Type of Item7: "+stu7.Item7.GetType());
           
        // Get the type of Item7
        // Using Type.GetGenericArguments method
        Type stu7type = stu7.GetType();
        Console.WriteLine("Type of Item7: "+stu7type.GetGenericArguments()[6]);
  
        Console.WriteLine();
          
    }
}
输出:
Student-7 Favourite Subject: DSA
Type of Item7: System.String
Type of Item7: System.String