How to get the Elements of the Nested ValueTuple in C#?
Last Updated :
28 Feb, 2022
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:
CSharp
using System;
class GFG {
static public void Main()
{
var ValTpl = ValueTuple.Create("Methods", "Method Hiding",
"Optional Parameters", "Anonymous Method",
"Partial Methods", "Local Function",
"Delegates", "Destructors");
Console.WriteLine("C# Topics: ");
Console.WriteLine(ValTpl.Item1);
Console.WriteLine(ValTpl.Item2);
Console.WriteLine(ValTpl.Item3);
Console.WriteLine(ValTpl.Item4);
Console.WriteLine(ValTpl.Item5);
Console.WriteLine(ValTpl.Item6);
Console.WriteLine(ValTpl.Item7);
Console.WriteLine(ValTpl.Rest);
}
}
|
Output:
C# Topics:
Methods
Method Hiding
Optional Parameters
Anonymous Method
Partial Methods
Local Function
Delegates
(Destructors)
Example 2:
CSharp
using System;
class GFG {
static public void Main()
{
var MyValTpl = ValueTuple.Create(23456, "Anu", "CSE", "14-4-1992",
27, 2010, 3456781234, ValueTuple.Create("Java", "C#"));
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#))
Share your thoughts in the comments
Please Login to comment...