C# - ValueTuple <T1,T2,T3,T4,T5,T6,T7,TRest> Struct
<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> struct in C# is part of the System namespace and is used to create n-ValueTuple where n = 8 or greater than 8. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The ValueTuple struct is a value type and mutable means the value of its elements can be modified. Some key features of tuples are mentioned below:
- Mutable: The value of the Item(fields) can be changed after the tuple is created.
- Type-Safe: The field is specified when the tuple is created, ensuring type safety.
- Implements Interfaces: <ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>, implements several interfaces such as:
- IStructuralComparable
- IStructuralEquatable
- IComparable<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>
- IEquatable<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>
- ITuple: This makes it useful in various comparison and equality scenarios.
Constructor
// Initializes a new ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest)
Fields
The constructor of the ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> struct initializes the tuple with seven values, Including the optional TRest that can represent additional elements, Accessed via fields ValueTuple<ElementNumber>.
- Item1: Retrieves the 1st element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item2: Retrieves the 2nd element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item3: Retrieves the 3rd element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item4: Retrieves the 4th element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item5: Retrieves the 5th element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item6: Retrieves the 6th element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- Item7: Retrieves the 7th element of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance.
- TRest: Any generic value tuple instance that defines the types of the tuple’s remaining elements.
Example:
// Accessing the element of ValueTuple<T1,
// T2, T3, T4, T5, T6, T7, TRest>
using System;
class Geeks
{
static public void Main()
{
// Creating a value tuple using Create method
var lib = ValueTuple.Create(3, "C#", "Geek",
2025, "Programming", "English", "India",
ValueTuple.Create("Java", "Javascript",
"Kotlin"));
// Display the element of the value-tuple
Console.WriteLine("Details: ");
Console.WriteLine("Book Id: {0}", lib.Item1);
Console.WriteLine("Book Name: {0}", lib.Item2);
Console.WriteLine("Author Name: {0}", lib.Item3);
Console.WriteLine("Publication date: {0}", lib.Item4);
Console.WriteLine("Gener: {0}", lib.Item5);
Console.WriteLine("Language: {0}", lib.Item6);
Console.WriteLine("Country: {0}", lib.Item7);
Console.WriteLine("Other Books Details: " + lib.Rest);
}
}
Output
Details: Book Id: 3 Book Name: C# Author Name: Geek Publication date: 2025 Gener: Programming Language: English Country: India Other Books Details: ((Java, Javascript, Kotlin))
Explanation: In the above example, we create a ValueTuple with seven elements and one nested valueTuple of different types and print each element by field Item<elementNumber> and nested valuetuple using ValutupleObject.Rest(Instance).
Methods
Methods | Description |
---|---|
CompareTo(ValueTuple) | Compares two tuples element by element and returns (0,1,-1) value based on their order. |
Equals(Object) | Check the Object equal to the current ValueTuple instance, return true if equals else return false |
Equals(ValueTuple) | Checks if two ValueTuple instances are equal by comparing their elements, return true if equals else return false |
GetHashCode() | Calculates the hash code for the current ValueTuple instance. |
ToString() | Returns a string that represents the value of current ValueTuple instance. |
Example:
// Demonstration of Equals() Method in ValueTuple
using System;
class Geeks
{
static public void Main()
{
// Creating 8-ValueTuple using Create method
var T1 = ValueTuple.Create(1, 2, 3, 4, 5, 6, 7 ,8);
var T2 = ValueTuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
// Check if both the value
// tuples are equal or not
if (T1.Equals(T2)) {
Console.WriteLine("Tuples Matched...!!");
}
else {
Console.WriteLine("Tuples Not Matched...!!");
}
}
}
Output
Tuples Matched...!!