C# | How to get the Remaining Elements of the Tuple?
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:
// 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)
Note: You can also use Rest property to get the elements of the nested tuple.
Example:
// 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))
Recommended Posts:
- How to create 3-Tuple or Triple Tuple in C#?
- How to create 1-Tuple or Singleton Tuple in C#?
- How to create 2-tuple or pair tuple in C#?
- C# | Tuple
- C# | How to get Third Element of the Tuple?
- C# | How to get the HashCode of the tuple?
- C# | Tuple<T1,T2> Class
- C# | How to get First Element of the Tuple?
- C# | How to get Second Element of the Tuple?
- How to create 6-Tuple in C#?
- C# | Tuple Class
- C# | Tuple<T1> Class
- C# | Tuple<T1,T2,T3,T4> Class
- C# | Tuple<T1,T2,T3,T4,T5> Class
- C# | Tuple<T1,T2,T3,T4,T5,T6> Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



