Last Updated : 11 Jul, 2025
ValueTuple<T1, T2, T3> struct in C# is part of the System namespace and is used to store a tuple with three values. It is a value type and provides better performance and memory management compared to the traditional Tuple class. Also, the ValueTuple<T1, T2, T3> is mutable which means the values of its elements can be modified after initialization.
ConstructorThe constructor for ValueTuple<T1, T2, T3> initializes the tuple with three values:
// Initializes a new ValueTuple<T1, T2, T3> instance
ValueTuple<T1, T2, T3>(T1, T2, T3)
This constructor creates an instance of ValueTuple<T1, T2, T3> using the specified values of types T1, T2, and T3.
FieldsValueTuple<T1, T2, T3> provides three properties, which correspond to its three elements:
Example: Accessing the elements of a ValueTuple<T1, T2, T3> in C#
C#
// Accessing the element of
// ValueTuple<T1, T2, T3>
using System;
class Geeks
{
static public void Main()
{
// Creating a value tuple using Create method
var t3 = ValueTuple.Create(1, "Geek", "C#");
// Display the element of the given value tuple
Console.WriteLine("Details: ");
Console.WriteLine("Id: {0}", t3.Item1);
Console.WriteLine("Name: {0}", t3.Item2);
Console.WriteLine("Subject: {0}", t3.Item3);
}
}
Details: Id: 1 Name: Geek Subject: C#
Explanation: In this example, a ValueTuple<T1, T2, T3> is created using the Create() method, where 1, "Geek", and "C#" are the three values. The program prints each value using the corresponding Item1, Item2, and Item3 properties.
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:
C#
// Check if value tuples are Equal
using System;
class Geeks
{
static public void Main()
{
// Creating 3-ValueTuple using Create method
var T1 = ValueTuple.Create(346, 784, 45);
var T2 = ValueTuple.Create(346, 784, 45);
var T3 = ValueTuple.Create(346, 784, 45);
// Check if all three value tuples are equal
if (T1.Equals(T2) && T2.Equals(T3))
{
Console.WriteLine("All tuples are equal.");
}
else
{
Console.WriteLine("Not all tuples are equal.");
}
}
}
All tuples are equal.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