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 or by the static Tuple.Create method.
- It implements the IStructuralComparable, IStructuralEquatable, and IComparable interface.
- It is defined under the System namespace.
- 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 parameter.
- 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>(T1, T2, T3, T4, T5, T6, T7)
Property
We can retrieve value of Tuple<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) object by read-only Item<elementNumber> instance property
- Item1: Gets the value of the first component of the tuple object.
- Item2: Gets the value of the second component of the tuple object.
- Item3: Gets the value of the third component of the tuple object
- Item4: Gets the value of the fourth component of the tuple object.
- Item5: Gets the value of the fifth component of the tuple object.
- Item6: Gets the value of the sixth component of the tuple object.
- Item7: Gets the value of the seventh component of the tuple object.
Example: Creating a 7-tuple using the constructor of the tuple class
// Using constructor and property of Tuple
// <T1, T2, T3, T4, T5, T6, T7> Class
using System;
class Geeks
{
static public void Main()
{
// Creating 7-Tuple using constructor
Tuple<int, int, int, string, int, string, int> tuple =
new Tuple<int,int, int, string, int, string, int>
(79, 34, 67, "Geeks",44, "GeeksforGeeks", 66);
// 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);
}
}
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
Methods
Method | Description |
---|---|
Returns a value that indicates whether the current Tuple object is equal to a specified object. | |
Returns a value that indicates whether the current Tuple object is equal to a specified object. | |
Returns a value that indicates whether the current Tuple object equals a specified object. | |
MemberwiseClone() | Creates a shallow copy of the current Object. |
ToString() | Creates a shallow copy of the current Object. |
Example 2: Demonstration of Equals() Method
// Using the tuple Equals() method
using System;
class Geeks
{
static public void Main()
{
// Creating 7-Tuple
// Using Tuple<T1, T2, T3, T4, T5, T6,
// T7>(T1, T2, T3, T4, T5, T6, T7) constructor
Tuple<int, int, int, int, int, int, int> mytuple1 =
new Tuple<int,int, int, int, int, int, int>
(20, 40, 90, 89, 33, 66, 87);
Tuple<int, int, int, int, int, int, int> mytuple2 =
new Tuple<int, int, int, int, int, int, int>
(20, 40, 90, 89, 33, 66, 87);
// Using Equals method
if (mytuple1.Equals(mytuple2))
Console.WriteLine("Tuple Matched.");
else
Console.WriteLine("Tuple Not Matched.");
}
}
Output
Tuple Matched.