Javatpoint Logo
Javatpoint Logo

ValueTuple in C#

ValueTuple is a new structure in C# 7.0 that represents the value type Tuple. It is already included in .NET Framework 4.7 or higher versions. It enables us to save a data collection that comprises several values that may or may not be connected. It can hold components ranging from 0 to 8, and elements of various types. Duplicate items can also be stored in value tuples.

Need of ValueTuple

In C#, we already have Tuples for storing multiple values, but Tuples have several constraints that ValueTuple overcomes. Alternatively, ValueTuple is an enhanced version of Tuples in C#. It gets over Tuples's drawbacks as follows:

  • Tuple is of the reference type, whereas ValueTuple is of the value type.
  • Tuple lacks naming conventions, but ValueTuple has strong naming standards.
  • Tuples do not enable us to build a tuple with zero components, whereas ValueTuple allows us to create a tuple having zero elements.
  • Tuple outperforms ValueTuple in terms of performance. ValueTuple is useful because it provides a lightweight technique for returning numerous values from existing methods. Furthermore, the syntax of ValueTuple has been more optimized than that of Tuples.
  • ValueTuple gives us additional access to the elements of value tuples by using deconstruction and the _ keyword. However, Tuple is unable to offer the _ keyword and the deconstruction concepts.
  • Components such as item1 and item2 are fields in ValueTuple. However, in Tuple, they contains properties.
  • Fields in ValueTuple are modifiable. However, fields in Tuple are only available for reading.

Making a ValueTuple

Unlike Tuple, ValueTuples provides an easy method for creating and initializing ValueTuples. ValueTuples may be created in three ways:

1. Using Constructor: We may generate ValueTuple by using the ValueTuple<T> Struct constructor, where we can save components numbered one through eight, along with their type.


It has the following syntax:


Let us take an example to illustrate how we can create a ValueTuple using a constructor in C#.


ValTpl1: 3456879
ValTpl2: C#, .Net, 5906
ValTpl3: 67, 64, 65, 32, 39, 37, 21, 96

2. Using the create Method:

When we use the constructor of the ValueTuple<T> struct to build a value tuple, we must supply the type of each element contained in the value tuple, which makes our code complex. As a result, C# provides another ValueTuple struct that has static methods for constructing value tuple objects without specifying the type of each element.


It has the following syntax:


Let us take an example to illustrate how we can create a ValueTuple using the Create method in C#.


Valtupl1s: ()
Valtupl2s: 15, 34, 44, 55
Valtupl3s: 39, Programming, j, f, j, 59.78, 431, java

3. Using parenthesis():

It is the most basic way for creating ValueTuples, and the items are inserted between them. The components are saved in two ways:

Named Member: ValueTuple lets us to create a tuple in which each component can have its name. As a result, we may access that component by using their name. It improves accessibility and recall of our program's code. We can assign names to members on either the left or right side, but not both. If we give names to both sides, the left side takes priority over the right side. As shown below:

Example 1:

Unnamed Member: Unnamed members are those in ValueTuples who do not have names. They are newly created with no name.

Example 2:

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA