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
How to get the remaining elements of the Tuple in C#?
The Rest property in C# is used to get the remaining elements of a Tuple when it contains more than 7 elements. Since Tuple can hold a maximum of 8 elements, the 8th position (accessed via the Rest property) contains any additional elements as a nested Tuple.
Syntax
Following is the syntax for accessing the Rest property of a Tuple −
var tuple = Tuple.Create(item1, item2, ..., item7, item8); var restElements = tuple.Rest;
How It Works
When a Tuple has exactly 8 elements, the Rest property returns the 8th element. When a Tuple has more than 8 elements, C# automatically creates nested Tuples, and the Rest property returns the nested Tuple containing elements beyond the first 7.
Using Rest Property with 8 Elements
Example
using System;
public class Demo {
public static void Main(String[] args) {
var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, 2000);
var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, 2000);
Console.WriteLine("Is Tuple1 equal to Tuple2? = " + tuple1.Equals(tuple2));
Console.WriteLine("HashCode of Tuple1 = " + tuple1.GetHashCode());
Console.WriteLine("HashCode of Tuple2 = " + tuple2.GetHashCode());
Console.WriteLine("Tuple1 Item 1st = " + tuple1.Item1);
Console.WriteLine("Tuple2 Item 1st = " + tuple2.Item1);
Console.WriteLine("Tuple1 Item 2nd = " + tuple1.Item2);
Console.WriteLine("Tuple2 Item 2nd = " + tuple2.Item2);
Console.WriteLine("Tuple1 Item 4th = " + tuple1.Item4);
Console.WriteLine("Tuple2 Item 4th = " + tuple2.Item4);
Console.WriteLine("Tuple1 Item 5th = " + tuple1.Item5);
Console.WriteLine("Tuple2 Item 5th = " + tuple2.Item5);
Console.WriteLine("Tuple1 Item 6th = " + tuple1.Item6);
Console.WriteLine("Tuple2 Item 6th = " + tuple2.Item6);
Console.WriteLine("Tuple1 Item 7th = " + tuple1.Item7);
Console.WriteLine("Tuple2 Item 7th = " + tuple2.Item7);
Console.WriteLine("Tuple1 rest value = " + tuple1.Rest);
Console.WriteLine("Tuple2 rest value = " + tuple2.Rest);
}
}
The output of the above code is −
Is Tuple1 equal to Tuple2? = True HashCode of Tuple1 = 3247155 HashCode of Tuple2 = 3247155 Tuple1 Item 1st = 75 Tuple2 Item 1st = 75 Tuple1 Item 2nd = 200 Tuple2 Item 2nd = 200 Tuple1 Item 4th = 700 Tuple2 Item 4th = 700 Tuple1 Item 5th = 100 Tuple2 Item 5th = 100 Tuple1 Item 6th = 1200 Tuple2 Item 6th = 1200 Tuple1 Item 7th = 1500 Tuple2 Item 7th = 1500 Tuple1 rest value = (2000) Tuple2 rest value = (2000)
Using Rest Property with Nested Tuples
Example
using System;
public class Demo {
public static void Main(String[] args) {
var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, Tuple.Create("AB", 2000, "CD"));
var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500, Tuple.Create(2500, 3500, 4000, "XY"));
Console.WriteLine("Is Tuple1 equal to Tuple2? = " + tuple1.Equals(tuple2));
Console.WriteLine("HashCode of Tuple1 = " + tuple1.GetHashCode());
Console.WriteLine("HashCode of Tuple2 = " + tuple2.GetHashCode());
Console.WriteLine("Tuple1 Item 1st = " + tuple1.Item1);
Console.WriteLine("Tuple2 Item 1st = " + tuple2.Item1);
Console.WriteLine("Tuple1 Item 2nd = " + tuple1.Item2);
Console.WriteLine("Tuple2 Item 2nd = " + tuple2.Item2);
Console.WriteLine("Tuple1 Item 4th = " + tuple1.Item4);
Console.WriteLine("Tuple2 Item 4th = " + tuple2.Item4);
Console.WriteLine("Tuple1 Item 5th = " + tuple1.Item5);
Console.WriteLine("Tuple2 Item 5th = " + tuple2.Item5);
Console.WriteLine("Tuple1 Item 6th = " + tuple1.Item6);
Console.WriteLine("Tuple2 Item 6th = " + tuple2.Item6);
Console.WriteLine("Tuple1 Item 7th = " + tuple1.Item7);
Console.WriteLine("Tuple2 Item 7th = " + tuple2.Item7);
Console.WriteLine("Tuple1 rest value = " + tuple1.Rest);
Console.WriteLine("Tuple2 rest value = " + tuple2.Rest);
}
}
The output of the above code is −
Is Tuple1 equal to Tuple2? = False HashCode of Tuple1 = -1121878415 HashCode of Tuple2 = -835095725 Tuple1 Item 1st = 75 Tuple2 Item 1st = 75 Tuple1 Item 2nd = 200 Tuple2 Item 2nd = 200 Tuple1 Item 4th = 700 Tuple2 Item 4th = 700 Tuple1 Item 5th = 100 Tuple2 Item 5th = 100 Tuple1 Item 6th = 1200 Tuple2 Item 6th = 1200 Tuple1 Item 7th = 1500 Tuple2 Item 7th = 1500 Tuple1 rest value = (AB, 2000, CD) Tuple2 rest value = (2500, 3500, 4000, XY)
Accessing Nested Tuple Elements
Example
using System;
public class Demo {
public static void Main(String[] args) {
var tuple = Tuple.Create(10, 20, 30, 40, 50, 60, 70, Tuple.Create("Hello", "World", 100));
Console.WriteLine("First 7 elements:");
Console.WriteLine("Item1: " + tuple.Item1);
Console.WriteLine("Item7: " + tuple.Item7);
Console.WriteLine("\nRest property (nested tuple): " + tuple.Rest);
// Accessing elements within the nested tuple
var nestedTuple = tuple.Rest;
Console.WriteLine("\nAccessing nested tuple elements:");
Console.WriteLine("Rest.Item1: " + nestedTuple.Item1);
Console.WriteLine("Rest.Item2: " + nestedTuple.Item2);
Console.WriteLine("Rest.Item3: " + nestedTuple.Item3);
}
}
The output of the above code is −
First 7 elements: Item1: 10 Item7: 70 Rest property (nested tuple): (Hello, World, 100) Accessing nested tuple elements: Rest.Item1: Hello Rest.Item2: World Rest.Item3: 100
Conclusion
The Rest property in C# Tuples provides access to elements beyond the first 7 positions in an 8-element Tuple. When the 8th element is a single value, Rest returns that value in parentheses. When the 8th element is a nested Tuple, Rest returns the entire nested Tuple, allowing access to multiple additional elements.
