Last Updated : 11 Jul, 2025
The Tuple<T1, T2> is used to create a 2-tuple or pair. It represents a tuple which contains the two elements in it. We can instantiate a Tuple<T1, T2> object by calling either the Tuple<T1, T2>(T1, T2) constructor or the static Tuple.Create method. We can retrieve the value of the tuple’s elements by using the read-only Item1 and Item2 instance properties. There are some important points which are mentioned below:
// Initializes a new instance of the Tuple<T1, T2> class.Properties
Tuple<T1, T2>(T1, T2)
Example 1: Creating a tuple to create new tuple.
C#
// Using constructor and property
// of Tuple<T1,T2> Class
using System;
class Geeks
{
static public void Main()
{
// Creating 2-Tuple
// Using Tuple<T1, T2>(T1, T2) constructor
Tuple<int, int> mytuple = new Tuple<int, int>(79, 80);
// Accessing the values
Console.WriteLine("Value of the First Component: " + mytuple.Item1);
Console.WriteLine("Value of the Second Component: " + mytuple.Item2);
}
}
Value of the First Component: 79 Value of the Second Component: 80Tuple Methods
Method
Description
Returns a value that indicates whether the current Tuple<T1, T2> object is equal to a specified object.
Returns the hash code for the current Tuple<T1, T2> object.
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> instance.
Example 2: Demonstration of Equals() Method.
C#
// Using the tuple Equals() method
using System;
public class Geeks
{
static public void Main()
{
// Creating 2-Tuple
// Using Tuple<T1, T2>(T1, T2) constructor
Tuple<int, int> mytuple1 = new Tuple<int, int>(20, 40);
Tuple<int, int> mytuple2 = new Tuple<int, int>(20, 49);
// Using Equals method
if (mytuple1.Equals(mytuple2))
Console.WriteLine("Tuple Matched..");
else
Console.WriteLine("Tuple not matched..");
}
}
Tuple not matched..
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4