Last Updated : 11 Jul, 2025
ValueTuple<T1, T2> is a struct in C# provided by the System namespace. It is used to create a tuple that stores a pair value tuple or 2-ValueTuple. The ValueTuple<T1, T2> Struct is a value type which means it is stored on the stack and it is mutable. We can change its value after creation. Tuple class provides better performance and memory management.
Creating a ValueTuple<T1, T2> InstanceValueTuple<T1, T2> does not have a publicly accessible constructor, so the preferred method of creating it is through the static Create() method, or by initializing the Item1 and Item2 fields directly.
Using the Create() Method:
// Creating a ValueTuple using Create() method
var vt = ValueTuple.Create(1, "Geek");
Direct Initialization:
Fields// Initializing a ValueTuple directly with Item1 and Item2
var vt = new ValueTuple<int, string> { Item1 = 1, Item2 = "Geek" };
ValueTuple<T1, T2> has the following fields:
These fields can be accessed and modified directly.
Example: Demonstration of access to the ValueTuple<T1,T2> element in C#.
C#
// Accessing ValueTuple<T1,T2> elements
using System;
class Geeks
{
static public void Main()
{
// Creating a value tuple
// Using Create method
var vt = ValueTuple.Create(1, "Geek");
// Display the element of the given value tuple
Console.WriteLine("Details: ");
Console.WriteLine("Id: {0}", vt.Item1);
Console.WriteLine("Name: {0}", vt.Item2);
}
}
Details: Id: 1 Name: Geek
Explanation: In the above example, it creates two ValueTuple of different types 1 and Geek, prints the value of Item1 and item2 to the console.
MethodsMethod
Description
CompareTo(ValueTuple)
It compares the current ValueTuple<T1> instance to a specified ValueTuple<T1> instance.
Equals(Object)
It returns a value that indicates whether the current ValueTuple<T1> instance equals a specified object.
Equals(ValueTuple<T1>)
It returns a value that indicates whether the current ValueTuple<T1> instance equals a specified ValueTuple<T1> instance.
GetHashCode()
It calculates the hash code for the current ValueTuple<T1> instance.
ToString()
It returns a string that represents the value of this ValueTuple<T1> instance.
Example: Checking if Two ValueTuples are Equal or not
C#
// Check if value tuples are Equal
using System;
class Geeks
{
static public void Main()
{
// Creating 2-ValueTuple
// Using Create method
var T1 = ValueTuple.Create(357,357);
var T2 = ValueTuple.Create(93, 40);
// Check if both the value tuples
// are equal or not
if (T1.Equals(T2))
Console.WriteLine("Tuple matched.");
else
Console.WriteLine("Tuple not matched.");
}
}
Tuple not matched.Key Features:
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