元组是一种数据结构,它为您提供最简单的方式来表示一个数据集,该数据集具有多个可能/可能彼此不相关的值。我们知道,通过使用Item
Rest属性允许您获取元组中除开始的七个元素以外的其余元素。
句法:
public TRest Rest { get; }
在此, TRest是当前Tuple
例子:
// C# program to illustrate the
// concept of Rest property
using System;
class GFG {
// Main Method
static public void Main()
{
// Creating 8-tuple
var stu = Tuple.Create("Mohan", 24, "CSE", 2016,
209, 235678909, "C#", 1);
// Accessing first element
Console.WriteLine("Student Name: " + stu.Item1);
// Accessing Second element
Console.WriteLine("Student Age: " + stu.Item2);
// Accessing third element
Console.WriteLine("Student Branch: " + stu.Item3);
// Accessing fourth element
Console.WriteLine("Student Passing Year: " + stu.Item4);
// Accessing fifth element
Console.WriteLine("Student Id: " + stu.Item5);
// Accessing sixth element
Console.WriteLine("Student Contact Number: " + stu.Item6);
// Accessing seventh element
Console.WriteLine("Student Fav Programming Language: " + stu.Item7);
// Accessing remaining element
// Using Rest property
Console.WriteLine("Student Rank: " + stu.Rest);
}
}
输出:
Student Name: Mohan
Student Age: 24
Student Branch: CSE
Student Passing Year: 2016
Student Id: 209
Student Contact Number: 235678909
Student Fav Programming Language: C#
Student Rank: (1)
注意:您还可以使用Rest属性获取嵌套元组的元素。
例子:
// C# program to illustrate how to access
// nested tuple using Rest property
using System;
class GFG {
// Main Method
static public void Main()
{
// Creating 8-tuple
var stu = Tuple.Create("Guriya", 24, "CSE", 2016, 209, 235678909,
1, Tuple.Create("C#", "C++", "Java", "Python"));
// Accessing first element
Console.WriteLine("Student Name: " + stu.Item1);
// Accessing Second element
Console.WriteLine("Student Age: " + stu.Item2);
// Accessing third element
Console.WriteLine("Student Branch: " + stu.Item3);
// Accessing fourth element
Console.WriteLine("Student Passing Year: " + stu.Item4);
// Accessing fifth element
Console.WriteLine("Student Id: " + stu.Item5);
// Accessing sixth element
Console.WriteLine("Student Contact Number: " + stu.Item6);
// Accessing seventh element
Console.WriteLine("Student Rank: " + stu.Item7);
// Accessing remaining element
// or accessing the elements of nested tuple
// Using Rest property
Console.WriteLine("Student Fav. Programming Language: " + stu.Rest);
}
}
输出:
Student Name: Guriya
Student Age: 24
Student Branch: CSE
Student Passing Year: 2016
Student Id: 209
Student Contact Number: 235678909
Student Rank: 1
Student Fav. Programming Language: ((C#, C++, Java, Python))