![]() |
VOOZH | about |
ValueTuple is a structure introduced in C# 7.0 which represents the value type. Already included in .NET Framework 4.7 or higher version. It allows us to store a data set that contains multiple values that may or may not be related to each other. It can store elements starting from 0 to 8 and can store elements of different types. We can also store duplicate elements in the value tuple.
We already have Tuples in C# which is used to store multiple values, but have some limitations, these limitations are fixed in ValueTuple. ValueTuple is an improved version of Tuples. It overcomes the following limitations of Tuples:
We can create ValueTuples by using the following ways:
We can create ValueTuple by using the constructor provided by the ValueTuple<T> Struct, where we can store elements starting from one to eight with their type.
Syntax:
// Constructor for creating one element
ValueTuple<T1>(T1)
// Constructor for creating two elements
ValueTuple<T1, T2>(T1, T2)
.
.
// Constructor for creating eight elements
ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest)
Example:
first element of ValueTuple: 345678 ValueTuple with three elements: C# Java 586 elements of ValueTuple with eight items: 45 67
The Create method is a simpler way to create ValueTuple objects without explicitly specifying the types of the tuple elements.
Syntax:
// Method for creating an empty value tuple
Create();
// Method for creating 1-ValueTuple
Create<T1>(T1)
.
.
// Method for creating 8-ValueTuple
Create<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, T8)
Example:
V1 is an empty ValueTuple V2 Item1: 12 V2 Item3: 40 V3 Item2: GeeksforGeeks V3 Item6: 56.78
It is the simplest form for creating ValueTuples using parenthesis "()" and the elements are placed in between them. The elements are stored in 2 different ways:
ValueTuple allows us to create a tuple in which each component is allowed to have a its own name. So we can access that component with the help of their name. It makes the program more readable. We can assign the names to the members on either the left-hand side or right-hand, but not both sides. If we assigned names to both sides, then the left-hand side has precedence over the right-hand side.
Example 1: Named Member
Author's age: 23 Author's name: Geek Author's language: C#
Example 2: Named Members Using var
Author Name: Geek Author Age: 23 Author Language: C#
In ValueTuples, the unnamed members are those members who do not have names. They are simply created without any name.
Example:
Author Age: 20 Author Name: Geek Author Language: C#
In ValueTuple, unnamed members are accessible by using the default item property names like Item1, Item2, Item3, etc. As shown in the below example:
Example:
Age:20 Name:Siya Language:Ruby
In ValueTuples, the named members are used according to their names. There is no need to access these named members with default item property. As shown in the below example, the ValueTuple contains three elements, i.e., Book_id, Author_name, and Book_name. And we directly access these elements according to their names.
Example:
Book Id: 2340 Author Name: Geek Book Name: C# tutorials
In C#, we are allowed to return a ValueTuple from a method. As shown in the below example, the TouristDetails method returns a ValueTuple with 3 elements.
Example:
Tourist Details: Tourist Id: 357 Tourist Name: Geek Country: USA