C# ValueTuple<T1,T2,T3> Struct
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.
Constructor
The 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.
Fields
ValueTuple<T1, T2, T3> provides three properties, which correspond to its three elements:
- Item1: Gets the value of the current ValueTuple<T1, T2, T3> instance’s first element.
- Item2: Gets the value of the current ValueTuple<T1, T2, T3> instance’s second element.
- Item3: Gets the value of the current ValueTuple<T1, T2, T3> instance’s third element.
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);
}
}
OutputDetails:
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.
Methods
Method | 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.");
}
}
}
OutputAll tuples are equal.
Key Features:
- Elements: A ValueTuple<T1, T2, T3> contains three elements, referred to as Item1, Item2 and Item3.
- Mutable: The value of Item1, Item2 and Item3 can be changed after the tuple is created.
- Type-Safe: The types of Item1, Item2 and Item3 are specified when the tuple is created, ensuring type safety.
- Duplicates: It also allows to store duplicate values.
- Implements Interfaces: ValueTuple<T1, T2, T3> implements several interfaces such as:
- IStructuralComparable
- IStructuralEquatable
- IComparable<ValueTuple<T1, T2, T3>>
- IEquatable<ValueTuple<T1, T2, T3>>
- ITuple: This makes it useful in various comparison and equality scenarios.
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers