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

📅  最后修改于: 2021-05-29 15:19:05             🧑  作者: Mango

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

句法:

public T2 Item2;

在此,T2是ValueTuple <>结构的字段值。此ValueTuple <>可以是2-ValueTuple或3-ValueTuple或4-ValueTuple或5-ValueTuple或6-ValueTuple或7-ValueTuple或8-ValueTuple。

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

// C# program to illustrate how to get
// the second element of value tuple
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        Console.WriteLine("C# Topics:");
          
        // Creating a value tuple 
        // with two elements
        var ValTpl2 = ValueTuple.Create("Array",
                                      "String");
  
        // Accessing the second element of 
        // 2-ValueTuple using Item property
        Console.WriteLine(ValTpl2.Item2);
  
        // Creating a value tuple
        // with three elements
        var ValTpl3 = ValueTuple.Create("ArrayList",
                                   "List", "Queue");
  
        // Accessing the second element of 
        // 3-ValueTuple using Item property
        Console.WriteLine(ValTpl3.Item2);
  
        // Creating a value tuple 
        // with four elements
        var ValTpl4 = ValueTuple.Create("Stack", "Dictionary",
                                   "LinkedList", "Interface");
  
        // Accessing the second element of 
        // 4-ValueTuple using Item property
        Console.WriteLine(ValTpl4.Item2);
  
        // Creating a value tuple with five elements
        var ValTpl5 = ValueTuple.Create("Identifiers", "Data Types",
                       "Keywords", "Access Modifiers", "Operators");
  
        // Accessing the second element of 
        // 5-ValueTuple using Item property
        Console.WriteLine(ValTpl5.Item2);
  
        // Creating a value tuple with six elements
        var ValTpl6 = ValueTuple.Create("Nullable Types", "Class",
            "Structure", "Indexers", "Switch Statement", "Loops");
  
        // Accessing the second element of 
        // 6-ValueTuple using Item property
        Console.WriteLine(ValTpl6.Item2);
  
        // Creating a value tuple with seven elements
        var ValTpl7 = ValueTuple.Create("Inheritance ", "Constructors", 
                        "Encapsulation", "Abstraction", "Static Class",
                                    "Partial Classes", "this keyword");
  
        // Accessing the second element of 
        // 7-ValueTuple using Item property
        Console.WriteLine(ValTpl7.Item2);
  
        // 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 second element of 
        // 8-ValueTuple using Item property
        Console.WriteLine(ValTpl8.Item2);
    }
}
输出:
C# Topics:
String
List
Dictionary
Data Types
Class
Constructors
Method Hiding