C# Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> Class
Last Updated :
21 Jan, 2025
Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> class is used to create an n-tuple where n = 8 or greater than 8. It represents a tuple that contains eight or more than eight elements in it. we can instantiate a Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object by calling either the Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest) constructor or by the static Tuple.Create method. There are some important points mentioned below:
- It implements IStructuralComparable, IStructuralEquatable, and IComparable interfaces.
- It is defined under the System namespace.
- We can create more than eight elements in a tuple with the help of nesting. You can create a nested tuple in the place of the Rest parameter in Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>.
- It represents multiple data into a single data set.
- It allows us to create, manipulate, and access data sets.
- It returns multiple values from a method without using out parameters.
- It allows the passing of multiple values to a method with the help of single parameters.
- It can also store duplicate elements.
Constructor
Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest)
Property
We can retrieve value of Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest) object by read-only Item<elementNumber> instance property
- Item1: Gets the value of the Tuple object’s first element.
- Item2: Gets the value of the Tuple object’s first element.
- Item3: Gets the value of the Tuple object’s first element.
- Item4: Gets the value of the Tuple object’s first element.
- Item5: Gets the value of the Tuple object’s first element.
- Item6: Gets the value of the Tuple object’s first element.
- Item7: Gets the value of the Tuple object’s first element.
- Rest: Gets the current Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object’s remaining elements
Example: Creating an 8-tuple using the constructor of the tuple class.
C#
// Using constructor and property
// of Tuple<T1, T2, T3, T4,
// T5, T6, T7, TRest> Class
using System;
class Geeks
{
static public void Main()
{
// Creating 8-Tuple using constructor
Tuple<int, int, int, string, int, string, int, Tuple<int, int>> tuple = new Tuple<int,
int, int, string, int, string, int, Tuple<int, int>>(79, 34, 67, "Geeks", 44,
"GeeksforGeeks", 66, new Tuple<int, int>(89, 77));
// Accessing the values
Console.WriteLine("Value of the First Component: " + tuple.Item1);
Console.WriteLine("Value of the Second Component: " + tuple.Item2);
Console.WriteLine("Value of the Third Component: " + tuple.Item3);
Console.WriteLine("Value of the Fourth Component: " + tuple.Item4);
Console.WriteLine("Value of the Fifth Component: " + tuple.Item5);
Console.WriteLine("Value of the Sixth Component: " + tuple.Item6);
Console.WriteLine("Value of the Seventh Component: " + tuple.Item7);
Console.WriteLine("Value of the Eighth Component: " + tuple.Rest);
}
}
Output:
Value of the First Component: 79
Value of the Second Component: 34
Value of the Third Component: 67
Value of the Fourth Component: Geeks
Value of the Fifth Component: 44
Value of the Sixth Component: GeeksforGeeks
Value of the Seventh Component: 66
Value of the Eighth Component: (89, 77)
Methods
Method | Description |
---|
Equals(Object) | Returns a value that indicates whether the current Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object is equal to a specified object. |
GetHashCode() | Returns the hash code for the current Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object. |
GetType() | Gets the Type of the current instance. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
ToString() | Returns a string that represents the value of this Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance |
Example: Demonstration of Equals() Method
C#
// Using the tuple Equals() method
using System;
class Geeks
{
static public void Main()
{
// Creating 8-Tuple Using constructor
Tuple<int, int, int, int, int, int, int, Tuple<string>> mytuple1 =
new Tuple<int,int, int, int, int, int, int, Tuple<string>>
(20, 40, 90, 89, 33, 66, 87, new Tuple<string>("GeeksforGeeks"));
Tuple<int, int, int, int, int, int, int, Tuple<string>> mytuple2 =
new Tuple<int,int, int, int, int, int, int, Tuple<string>>
(20, 40, 90, 89, 33, 66, 87, new Tuple<string>("GeeksforGeeks"));
// Using Equals method
if (mytuple1.Equals(mytuple2))
Console.WriteLine("Tuple Matched.");
else
Console.WriteLine("Tuple Not Matched.");
}
}
Similar Reads
C# Tuple<T1,T2,T3,T4,T5,T6,T7> Class Tuple<T1, T2, T3, T4, T5, T6, T7> class creates a 7-tuple or septuple. It represents a tuple that contains seven elements. We can instantiate a Tuple<T1, T2, T3, T4, T5, T6, T7> object by calling either the Tuple<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) constructor o
3 min read
C# Tuple<T1,T2,T3,T4,T5,T6> Class Tuple<T1, T2, T3, T4, T5, T6> class creates a 6-tuple or sextuple. It represents a tuple that contains six elements. You can instantiate a Tuple<T1, T2, T3, T4, T5, T6> object by calling either the Tuple<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6) constructor or by the static T
3 min read
C# Tuple<T1,T2,T3,T4,T5> Class Tuple<T1, T2, T3, T4, T5> class creates a 5 Tuple or quintuple. It represents a tuple that contains five elements. We can instantiate a Tuple<T1, T2, T3, T4, T5> object by calling either the Tuple<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5) constructor or by the static Tuple.Create met
3 min read
C# - ValueTuple <T1,T2,T3,T4,T5,T6,T7,TRest> Struct <ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> struct in C# is part of the System namespace and is used to create n-ValueTuple where n = 8 or greater than 8. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory managemen
3 min read
C# Tuple<T1,T2,T3,T4> Class Tuple<T1, T2, T3, T4> class creates a 4-tuple or quadruple. It represents a tuple that contains four elements. we can instantiate a Tuple<T1, T2, T3, T4> object by calling either the Tuple<T1, T2, T3, T4>(T1, T2, T3, T4) constructor or by the static Tuple.Create method.It implement
2 min read