C# | How to get the Remaining Elements of the Tuple?
Last Updated :
01 May, 2019
Tuple is a data structure which gives you the easiest way to represent a data set which has multiple values that may/may not be related to each other. As we know that by using Item<ElementNumber> property we can get the elements present in the tuples, but this property works only for seven elements. If you want to get the remaining element then you must go for Rest property.
Rest property allows you to get the remaining element of the tuple other than the starting seven elements.
Syntax:
public TRest Rest { get; }
Here, TRest is the value of the current Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object’s remaining components.
Example:
using System;
class GFG {
static public void Main()
{
var stu = Tuple.Create( "Mohan" , 24, "CSE" , 2016,
209, 235678909, "C#" , 1);
Console.WriteLine( "Student Name: " + stu.Item1);
Console.WriteLine( "Student Age: " + stu.Item2);
Console.WriteLine( "Student Branch: " + stu.Item3);
Console.WriteLine( "Student Passing Year: " + stu.Item4);
Console.WriteLine( "Student Id: " + stu.Item5);
Console.WriteLine( "Student Contact Number: " + stu.Item6);
Console.WriteLine( "Student Fav Programming Language: " + stu.Item7);
Console.WriteLine( "Student Rank: " + stu.Rest);
}
}
|
Output:
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)
Note: You can also use Rest property to get the elements of the nested tuple.
Example:
using System;
class GFG {
static public void Main()
{
var stu = Tuple.Create( "Guriya" , 24, "CSE" , 2016, 209, 235678909,
1, Tuple.Create( "C#" , "C++" , "Java" , "Python" ));
Console.WriteLine( "Student Name: " + stu.Item1);
Console.WriteLine( "Student Age: " + stu.Item2);
Console.WriteLine( "Student Branch: " + stu.Item3);
Console.WriteLine( "Student Passing Year: " + stu.Item4);
Console.WriteLine( "Student Id: " + stu.Item5);
Console.WriteLine( "Student Contact Number: " + stu.Item6);
Console.WriteLine( "Student Rank: " + stu.Item7);
Console.WriteLine( "Student Fav. Programming Language: " + stu.Rest);
}
}
|
Output:
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))
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...