📜  如何在C#中获取ValueTuple的第六个元素?

📅  最后修改于: 2021-05-30 00:20:48             🧑  作者: Mango

ValueTuple是C#7.0中引入的结构,表示值类型Tuple。它允许您存储一个数据集,该数据集包含可能彼此相关或不相关的多个值。 Item6属性用于获取给定值元组的第六个未命名元素。它适用于每个值元组,例如6-ValueTuple,7-ValueTuple等。

句法:

public T6 Item6;

在此,T6是ValueTuple <>结构的字段值。该ValueTuple <>可以是6-ValueTuple,7-ValueTuple或8-ValueTuple。

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

// C# program to illustrate how to get
// the sixth element of value tuple
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        Console.WriteLine("C# Topics:");
  
        // Creating a value tuple with six elements
        var ValTpl6 = ValueTuple.Create("Nullable Types", "Class",
            "Structure", "Indexers", "Switch Statement", "Loops");
  
        // Accessing the sixth element of 
        // 6-ValueTuple using Item property
        Console.WriteLine(ValTpl6.Item6);
  
        // Creating a value tuple with seven elements
        var ValTpl7 = ValueTuple.Create("Inheritance ", "Constructors", 
                        "Encapsulation", "Abstraction", "Static Class",
                                    "Partial Classes", "this keyword");
  
        // Accessing the sixth element of 
        // 7-ValueTuple using Item property
        Console.WriteLine(ValTpl7.Item6);
  
        // Creating a value tuple with eight elements
        var ValTpl8 = ValueTuple.Create("Methods", "Method Hiding",
                        "Optional Parameters", "Anonymous Method",
                "Partial Methods", "Local Function", "Delegates",
                                                    "Destructors");
  
        // Accessing the sixth element of 
        // 8-ValueTuple using Item property
        Console.WriteLine(ValTpl8.Item6);
    }
}
输出:
C# Topics:
Loops
Partial Classes
Local Function

范例2:

// C# program to get the hash code of
// sixth element in a value tuple
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 6-ValueTuple
        var My_Value_Tuple = (1004, "Rohit", "Computer Science", 
                                                24, "C#", 2017);
  
        // Accessing sixth element of the value tuple
        Console.WriteLine("Passing Year: {0}", 
                        My_Value_Tuple.Item6);
  
        // Getting the hashcode of the sixth element
        Console.WriteLine("Hash Code: {0}",
           My_Value_Tuple.Item6.GetHashCode());
    }
}
输出:
Passing Year: 2017
Hash Code: 2017