C# ValueTuple Struct
ValueTuple Struct in C# is a structure that provides static methods that are used in creating value tuples. It is defined under the System namespace and was introduced in .NET Framework 4.7. This struct enables runtime implementation tuples in C#. The ValueTuple structure represents a tuple that can contain from 0 to 8 elements.
Key Differences Between ValueTuple and Tuple
ValueTuple is different from the Tuple class in the following terms:
- It is of value type, not reference type.
- It is mutable rather than read-only.
- In ValueTuple, item1, item2, item3, etc. data members are fields rather than properties.
Note: ValueTuple implements IStructuralComparable, IStructuralEquatable, IComparable, IComparable<ValueTuple>, IEquatable<ValueTuple>, and ITuple interfaces.
Declare and Create ValueTuple
We can declare a ValueTuple using different ways some of which are shown below:
// Using parenthesis
var person = (1, "Geek", "C#", 3.0);// Using the Create Method:
var person = ValueTuple.Create (1, "Geek", "C#", 3.0);// Using Constructor
var T1 = new ValueTuple<int, string, double>(2, "Coder", 2.71);
Example: Different Ways to Create ValueTuples
// Creating ValueTuple in C#
using System;
class Geeks
{
static public void Main()
{
// Creating ValueTuple using parentheses
var person = (1, "Geek", "C#", 3.0);
// Creating ValueTuple using constructor
var T1 = new ValueTuple<int, string, double>(1, "Coder", 2.71);
// Creating ValueTuple by specifying types
(int, string) T2 = (1, "Geeks");
// Accessing elements of ValueTuple
Console.WriteLine(T1.Item1);
Console.WriteLine(T2.Item1);
Console.WriteLine(person.Item1);
}
}
Output:

Note: Creating tuple Using parenthesis() was introduced in C# 7.0. We have version earlier than C# 7.0, this syntax will not be recognized and will result in a compilation error.
Key Features of ValueTuple
- Stack Allocation: Unlike tuple stores in the heap, ValueTuple is stored in the Stack and provides efficient memory allocation.
- Mutable: By default ValueTuple is mutable and provides more flexibility than tuple.
- Multiple Datatypes: Similar to tuple we can store values of different datatypes.
- Efficiency: It stores memory in the stack which makes it more efficient in terms of memory allocation.
- Deconstruction: ValueTuple supports Deconstruction and allows to assignment of separate values to each element.
ValueTuple Methods
Method | Description |
---|---|
Create() | Creates a new value tuple with zero components. |
CompareTo(ValueTuple) | Compares the current ValueTuple instance to a specified ValueTuple instance. |
ToString() | Returns the string representation of this ValueTuple instance. |
Returns the hash code for the current ValueTuple instance. | |
Returns a value indicating whether the current ValueTuple instance equals a specified object. | |
Equals(ValueTuple) | Determines whether two ValueTuple instances are equal. This method always returns true. |
Create<T1>(T1) | Creates a new value tuple with 1 component (a singleton). |
Create<T1, T2>(T1, T2) | Creates a new value tuple with 2 components (a pair). |
Create<T1, T2, T3>(T1, T2, T3) | Creates a new value tuple with 3 components (a triple). |
Create<T1, T2, T3, T4>(T1, T2, T3, T4) | Creates a new value tuple with 4 components (a quadruple). |
Create<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5) | Creates a new value tuple with 5 components (a quintuple). |
Create<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6) | Creates a new value tuple with 6 components (a sextuple). |
Create<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) | Creates a new value tuple with 7 components (a septuple). |
Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1, T2, T3, T4, T5, T6, T7, T8) | Creates a new value tuple with 8 components (an octuple). |
Example: Demonstration of Using Different methods on ValueTuple in C#.
// Demonstrating ValueTuple Methods in C#
using System;
class Geeks
{
static public void Main()
{
// Creating a value tuple with zero
// elements using Create method
var t1 = ValueTuple.Create();
// Displaying the hash code of the empty value tuple
Console.WriteLine("HashCode of a value tuple with " +
"zero elements: " + t1.GetHashCode());
// Creating value tuples with two elements each
var t2 = ValueTuple.Create(56, 3);
var t3 = ValueTuple.Create(56, 45);
// Comparing two value tuples using CompareTo method
int res1 = t2.CompareTo(t3);
// Displaying the comparison result
Console.WriteLine("CompareTo Method Result: " + res1);
}
}
Output
HashCode of a value tuple with zero elements: 0 CompareTo Method Result: -1