C# ValueTuple <T1,T2,T3,T4,T5,T6,T7> Struct
Last Updated :
21 Jan, 2025
ValueTuple <T1, T2, T3, T4, T5, T6, T7> struct in C# is part of the System namespace and is used to create a tuple that stores septuple or 7-ValueTuple. 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 that the Tuple provides 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> implements several interfaces such as:
- IStructuralComparable
- IStructuralEquatable
- IComparable<ValueTuple<T1, T2, T3, T4, T5, T6, T7>>
- IEquatable<ValueTuple<T1, T2, T3, T4, T5, T6, T7>>
- ITuple: This makes it useful in various comparison and equality scenarios.
Constructor
// Initializes a new ValueTuple<T1, T2, T3, T4, T5, T6, T7> instance.
ValueTuple<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6, T7)
Fields
The constructor of this ValueTuple initializes the tuple with seven values. It is not a property, it is a field that can be accessed by ValueTuple<ElementNumber>
- Item1: Retrieves the 1st element of ValueTuple<T1, T2, T3, T4, T5, T6, T7> instance.
- Item2: Retrieves the 2nd element of ValueTuple<T1, T2, T3, T4, T5, T6, T7> instance.
- Item3: Retrieves the 3rd element of ValueTuple<T1, T2, T3, T4, T5, T6, T7> instance.
- Item4: Retrieves the 4th element of ValueTuple<T1, T2, T3, T4, T5, T6, T7> instance.
- Item5: Retrieves the 5th element of ValueTuple<T1, T2, T3, T4, T5, T6, T7> instance.
- Item6: Retrieves the 6th element of ValueTuple<T1, T2, T3, T4, T5, T6, T7> instance.
- Item7: Retrieves the 7th element of ValueTuple<T1, T2, T3, T4, T5, T6, T7> instance.
Example: Demonstration of access to the ValueTuple<T1, T2, T3, T4, T5, T6, T7> element in C#.
C#
// Accessing elements of
// ValueTuple<T1, T2, T3, T4, T5, T6, T7>
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");
// Display the element of the value-tuple
Console.WriteLine("Book 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);
}
}
OutputBook Details:
Book Id: 3
Book Name: C#
Author Name: Geek
Publication date: 2025
Gener: Programming
Language: English
Country: India
Explanation: In the above example create a ValueTuple with seven elements of different types and print each element by field Item<elementNumber>.
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 of Equals() Method in ValueTuple
C#
// Check if two value tuples are equal
using System;
class Geeks
{
static public void Main()
{
// Creating 7-ValueTuple
// using Create method
var T1 = ValueTuple.Create(1, 2, 3, 4, 5, 6, 7);
var T2 = ValueTuple.Create(1, 2, 3, 4, 5, 6, 7);
// Check if both the value
// tuples are equal or not
if (T1.Equals(T2)) {
Console.WriteLine("Tuples Matched..!!");
}
else {
Console.WriteLine("Tuples Not Matched..!!");
}
}
}
Similar Reads
C# ValueTuple <T1,T2,T3,T4,T5,T6> Struct ValueTuple <T1, T2, T3, T4, T5, T6> struct in C# is part of the System namespace and is used to create a tuple that storessextuple or 6-ValueTuple. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The ValueT
3 min read
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 managemen
3 min read
C# ValueTuple <T1,T2,T3,T4,T5> Struct ValueTuple <T1, T2, T3, T4, T5> struct in C# is part of the System namespace and is used to create a tuple that stores quintuple or 5-ValueTuple. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The ValueTup
3 min read
C# ValueTuple <T1,T2,T3,T4> Struct ValueTuple <T1, T2, T3, T4> struct in C# is part of the System namespace and it is used to create a tuple that stores quadruple or 4-ValueTuple. It offers a more lightweight, value-type tuple compared to the older Tuple class and provides better performance and memory management. The ValueTupl
3 min read
C# ValueTuple<T1,T2,T3> Struct 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 th
3 min read
C# ValueTuple<T1, T2> Struct 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 creatio
3 min read