ValueTuple是C#7.0中引入的结构,表示值类型Tuple。它允许您存储一个数据集,该数据集包含可能彼此相关或不相关的多个值。众所周知, Item
Rest属性允许您获取值元组的剩余元素,而不是开始的七个元素。或者您可以说它用于访问嵌套值元组的元素。
句法:
public TRest Rest;
在此,TRest是ValueTuple
范例1:
// C# program to illustrate how to get
// the eigth element of value tuple
using System;
class GFG {
// Main Method
static public void Main()
{
// Creating a value tuple with eight elements
var ValTpl = ValueTuple.Create("Methods", "Method Hiding",
"Optional Parameters", "Anonymous Method",
"Partial Methods", "Local Function",
"Delegates", "Destructors");
Console.WriteLine("C# Topics: ");
// Accessing the first element of 8-ValueTuple
// Using Item property
Console.WriteLine(ValTpl.Item1);
// Accessing the second element of 8-ValueTuple
// Using Item property
Console.WriteLine(ValTpl.Item2);
// Accessing the third element of 8-ValueTuple
// Using Item property
Console.WriteLine(ValTpl.Item3);
// Accessing the fourth element of 8-ValueTuple
// Using Item property
Console.WriteLine(ValTpl.Item4);
// Accessing the fifth element of 8-ValueTuple
// Using Item property
Console.WriteLine(ValTpl.Item5);
// Accessing the sixth element of 8-ValueTuple
// Using Item property
Console.WriteLine(ValTpl.Item6);
// Accessing the seventh element of 8-ValueTuple
// Using Item property
Console.WriteLine(ValTpl.Item7);
// Accessing the eighth element of 8-ValueTuple
// Using Rest property
Console.WriteLine(ValTpl.Rest);
}
}
输出:
C# Topics:
Methods
Method Hiding
Optional Parameters
Anonymous Method
Partial Methods
Local Function
Delegates
(Destructors)
范例2:
// C# program to get the
// elements of nested value tuple
using System;
class GFG {
// Main method
static public void Main()
{
// Creating a nested value tuple
// Using ValueTuple(T1, T2, T3, T4, T5, T6, T7,
// TRest rest) constructor
var MyValTpl = ValueTuple.Create(23456, "Anu", "CSE", "14-4-1992",
27, 2010, 3456781234, ValueTuple.Create("Java", "C#"));
// Accessing the elements of the value tuple
Console.WriteLine("Employee Details:");
Console.WriteLine("Id: {0}", MyValTpl.Item1);
Console.WriteLine("Name: {0}", MyValTpl.Item2);
Console.WriteLine("Branch: {0}", MyValTpl.Item3);
Console.WriteLine("DOB: {0}", MyValTpl.Item4);
Console.WriteLine("Age: {0}", MyValTpl.Item5);
Console.WriteLine("Passing Year: {0}", MyValTpl.Item6);
Console.WriteLine("Phone Number: {0}", MyValTpl.Item7);
Console.WriteLine("Programming Language: {0}", MyValTpl.Rest);
}
}
输出:
Employee Details:
Id: 23456
Name: Anu
Branch: CSE
DOB: 14-4-1992
Age: 27
Passing Year: 2010
Phone Number: 3456781234
Programming Language: ((Java, C#))