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 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>(T1, T2, T3, T4)
Property
We can retrieve value of Tuple<T1, T2, T3, T4> 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.
Example:
using System;
class Geeks
{
static public void Main()
{
// Creating 4-Tuple using constructor
Tuple<int, int, int, int> t1 = new Tuple<int,
int, int, int>(20, 40, 90, 89);
Tuple<int, int, int, int> t2 = new Tuple<int,
int, int, int>(20, 40, 90, 89);
// Using Equals method
if (t1.Equals(t2))
Console.WriteLine("Tuple Matched..");
else
Console.WriteLine("Tuple not Matched..");
}
}
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
Methods
There are some important methods which are used in tuples are given below:
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() | Returns a string that represents the value of this Tuple<T1, T2, T3, T4> instance. |
Example:
using System;
class Geeks
{
static public void Main()
{
// Creating 4-Tuple using constructor
Tuple<int, int, int, int> mytuple1 = new Tuple<int,
int, int, int>(20, 40, 90, 89);
Tuple<int, int, int, int> mytuple2 = new Tuple<int,
int, int, int>(20, 40, 90, 89);
// Using Equals method
if (mytuple1.Equals(mytuple2))
Console.WriteLine("Tuple Matched..");
else
Console.WriteLine("Tuple not Matched..");
}
}
Output
Tuple Matched..