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

📅  最后修改于: 2021-05-30 01:26:03             🧑  作者: Mango

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

句法:

public T6 Item6 { get; }

在此, T6是当前Tuple <>对象的第六个分量的值。该Tuple <>可以是6元组,7元组或8元组。

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

// C# program to illustrate how to get 
// the sixth element of the tuple
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Taking 6-tuple
        var st6 = Tuple.Create("Riya", 24, "ME", 2015,
                               "30-May-1991", 230134832);
  
        Console.WriteLine("Student-6 Contact No.: " + st6.Item6);
  
        // Taking 7-tuple
        var st7 = Tuple.Create("Rohit", 21, "IT", 2017, 
                        "21-Apr-1994", 384749829, 20000);
  
        Console.WriteLine("Student-7 Contact No.: " + st7.Item6);
  
        // Taking 8-tuple
        var st8 = Tuple.Create("Manita", 24, "CSE", 2013, 
                   "03-Aug-1991", 235678909, 34000, "C#");
  
        Console.WriteLine("Student-8 Contact No.: " + st8.Item6);
    }
}
输出:
Student-6 Contact No.: 230134832
Student-7 Contact No.: 384749829
Student-8 Contact No.: 235678909

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

例子:

// C# program to illustrate how to get the 
// type of the Sixth element of the tuple
using System;
   
class GFG{
       
    // Main method
    static public void Main (){
           
        // Taking 6-tuple
        var stu6 = Tuple.Create("Riya", 24, "CSE", 2015, 102, 230134832);
        Console.WriteLine("Student-6 Contact Number: "+stu6.Item6);
           
        // Get the type of Item6
        // Using GetType() method
        Console.WriteLine("Type of Item6: "+stu6.Item6.GetType());
           
        // Get the type of Item6
        // Using Type.GetGenericArguments method
        Type stu6type = stu6.GetType();
        Console.WriteLine("Type of Item6: "+stu6type.GetGenericArguments()[5]);
  
        Console.WriteLine();
          
    }
}
输出:
Student-6 Contact Number: 230134832
Type of Item6: System.Int32
Type of Item6: System.Int32