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:
- 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.
Syntax:
It has the following syntax:
//Constructor
ValueTuple<Tup1>(Tup1)
//The two elements are created
ValueTuple<Tup1, Tup2>(Tup1, Tup2)
.
.
.
//The other elements are created
ValueTuple<Tup1, Tup2, Tup3, Tup4, Tup5, Tup6, Tup7, TRest>(Tup1, Tup2, Tup3, Tup4, Tup5, Tup6, Tup7, TRest)
Example:
Let us take an example to illustrate how we can create a ValueTuple using a constructor in C#.
using System;
class ValueTupleExample {
// Main method
static public void Main()
{
// The tuple having one element
ValueTuple<int> ValTpl1 = new ValueTuple<int>(3456879);
// The tuple having three elements
ValueTuple<string, string, int> ValTpl2 = new ValueTuple<string, string, int>("C#", ".Net", 5906);
// The value tuple consisting of 8 elements
ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> ValTpl3 = new ValueTuple<int,
int, int, int, int, int, int, ValueTuple<int>>(67, 64, 65, 32, 39, 37, 21, new ValueTuple<int>(96));
// Printing out the content of tuples
Console.WriteLine($"ValTpl1: {ValTpl1.Item1}");
Console.WriteLine($"ValTpl2: {ValTpl2.Item1}, {ValTpl2.Item2}, {ValTpl2.Item3}");
Console.WriteLine($"ValTpl3: {ValTpl3.Item1}, {ValTpl3.Item2}, {ValTpl3.Item3}, {ValTpl3.Item4}, {ValTpl3.Item5}, {ValTpl3.Item6}, {ValTpl3.Item7}, {ValTpl3.Rest.Item1}");
}
}
Output:
ValTpl1: 3456879
ValTpl2: C#, .Net, 5906
ValTpl3: 67, 64, 65, 32, 39, 37, 21, 96
- 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.
Syntax:
It has the following syntax:
// An empty tuple creation
Create();
// the method for creating 1 tuple
Create<Tup1>(Tup1)
.
.
.
// Method which is used for the creation of 8 tuples
Create<Tup1, Tup2, Tup3, Tup4, Tup5, Tup6, Tup7, TRest>(Tup1, Tup2, Tup3, Tup4, Tup5, Tup6, Tup7, Tup8)
Example:
Let us take an example to illustrate how we can create a ValueTuple using the Create method in C#.
using System;
public class CreateMethod{
// Main method
static public void Main()
{
// the create() method for tuple creation
var Valtupl1s = ValueTuple.Create();
// the Create(t1,t2,t3) method for creating 3 tuples
var Valtupl2s = ValueTuple.Create(15, 34, 44, 55);
// Making an 8-ValueTuple // Using the Create(Tup1, Tup2, Tup3, Tup4, Tup5, Tup6, Tup7, Tup8) Method
var Valtupl3s = ValueTuple.Create(39, "Programming",
'j', 'f', 'j', 59.78, 431, "java");
// display of the tuple elements
Console.WriteLine($"Valtupl1s: {Valtupl1s}");
Console.WriteLine($"Valtupl2s: {Valtupl2s.Item1}, {Valtupl2s.Item2}, {Valtupl2s.Item3}, {Valtupl2s.Item4}");
Console.WriteLine($"Valtupl3s: {Valtupl3s.Item1}, {Valtupl3s.Item2}, {Valtupl3s.Item3}, {Valtupl3s.Item4}, {Valtupl3s.Item5}, {Valtupl3s.Item6}, {Valtupl3s.Item7}, {Valtupl3s.Rest.Item1}");
}
}
Output:
Valtupl1s: ()
Valtupl2s: 15, 34, 44, 55
Valtupl3s: 39, Programming, j, f, j, 59.78, 431, java
- 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:
using System;
public class Namespace {
static public void Main()
{
(int ages, string Anames, string Language) authors = (232, "Sony", "C#");
}
}
Unnamed Member: Unnamed members are those in ValueTuples who do not have names. They are newly created with no name.
Example 2:
using System;
public class Names{
static public void Main()
{
var authors = (22, "Sriya", "Rohan");
}
}