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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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))


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.