Skip to content
Related Articles

Related Articles

Improve Article

How to get the Elements of the Nested ValueTuple in C#?

  • Last Updated : 23 Jul, 2019
Geek Week

ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other. As we know that Item<ElementNumber> property is used to get the elements present in the value tuples, but this property works only for seven elements. If you want to get the remaining elements, then you must go for Rest property.
The Rest property allows you to get the remaining element of the value tuple other than the starting seven elements. Or you can say it is used for accessing the elements of the nested value tuple.

Syntax:

public TRest Rest;

Here, TRest is the field value of a ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> structure.

Example 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);
    }
}
Output:
C# Topics: 
Methods
Method Hiding
Optional Parameters
Anonymous Method
Partial Methods
Local Function
Delegates
(Destructors)

Example 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>(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);
    }
}
Output:
Employee Details:
Id: 23456
Name: Anu
Branch: CSE
DOB: 14-4-1992
Age: 27
Passing Year: 2010
Phone Number: 3456781234
Programming Language: ((Java, C#))



My Personal Notes arrow_drop_up
Recommended Articles
Page :