Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Tuple Rest Property in C#
The Rest property in C# tuples allows you to create tuples with eight or more elements by nesting additional tuple objects. When a tuple has more than seven elements, the eighth and subsequent elements are stored in the Rest property as a nested tuple.
Syntax
The general structure for an 8-element tuple using the Rest property is −
Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>
Where TRest is another tuple type containing the remaining elements. You can create such tuples using Tuple.Create() −
var myTuple = Tuple.Create(item1, item2, item3, item4, item5, item6, item7, item8);
How the Rest Property Works
When you create a tuple with eight or more elements, C# automatically wraps the extra elements in a nested tuple stored in the Rest property. The first seven elements are accessed normally using Item1 through Item7, while elements 8 and beyond are accessed through the Rest property.
Example with 8-Element Tuple
using System;
public class Program {
public static void Main() {
var myTuple = Tuple.Create(1, 2.5M, "Tom", "100", 5, 10.5M, "Henry", "200");
Console.WriteLine("Item1 : " + myTuple.Item1);
Console.WriteLine("Item2 : " + myTuple.Item2);
Console.WriteLine("Item3 : " + myTuple.Item3);
Console.WriteLine("Item4 : " + myTuple.Item4);
Console.WriteLine("Item5 : " + myTuple.Item5);
Console.WriteLine("Item6 : " + myTuple.Item6);
Console.WriteLine("Item7 : " + myTuple.Item7);
Console.WriteLine("Rest Property : " + myTuple.Rest);
Console.WriteLine("Item8 (via Rest) : " + myTuple.Rest.Item1);
}
}
The output of the above code is −
Item1 : 1 Item2 : 2.5 Item3 : Tom Item4 : 100 Item5 : 5 Item6 : 10.5 Item7 : Henry Rest Property : (200) Item8 (via Rest) : 200
Example with More Than 8 Elements
using System;
public class Program {
public static void Main() {
var largeTuple = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Console.WriteLine("First 7 elements:");
Console.WriteLine("Item1-7: " + largeTuple.Item1 + ", " + largeTuple.Item2 + ", " +
largeTuple.Item3 + ", " + largeTuple.Item4 + ", " + largeTuple.Item5 +
", " + largeTuple.Item6 + ", " + largeTuple.Item7);
Console.WriteLine("\nRest property contains:");
Console.WriteLine("Rest: " + largeTuple.Rest);
Console.WriteLine("Item8: " + largeTuple.Rest.Item1);
Console.WriteLine("Item9: " + largeTuple.Rest.Item2);
Console.WriteLine("Item10: " + largeTuple.Rest.Item3);
}
}
The output of the above code is −
First 7 elements: Item1-7: 1, 2, 3, 4, 5, 6, 7 Rest property contains: Rest: (8, 9, 10) Item8: 8 Item9: 9 Item10: 10
Key Points
-
The
Restproperty is automatically used when you create tuples with 8 or more elements. -
Elements 8 and beyond are accessed via
Rest.Item1,Rest.Item2, etc. -
The
Restproperty itself is a tuple containing the remaining elements. -
This nesting allows for theoretically unlimited tuple sizes, though performance may degrade with very large tuples.
Conclusion
The Rest property in C# tuples enables creating tuples with eight or more elements by automatically nesting additional elements in a separate tuple. While useful for handling large sets of related data, consider using custom classes or structs for better readability and performance when dealing with many elements.
