How to use ValueTuples in C#

By FoxLearn 1/3/2025 4:33:50 AM   82
A Tuple is a data structure consisting of an ordered, fixed-size collection of immutable elements, each of a specific type that cannot be modified.

Introduced in C# 7.0, a ValueTuple addresses two main drawbacks of Tuples: inefficiency and the need to reference elements as Item1, Item2, etc. Unlike Tuples, ValueTuples are more efficient, allow for custom naming of elements, and provide better performance.

ValueTuple vs Tuple in C#

A ValueTuple is a lightweight, value-type structure that returns multiple values from a method. Unlike Tuples, which are reference types, ValueTuples have optimized syntax, support custom naming, and allow mutable fields, offering better performance. Additionally, while Tuple properties are read-only, ValueTuple properties can be modified after creation.

Install the System.ValueTuple NuGet package

After creating a console application in Visual Studio, you need to install the System.ValueTuple NuGet package.

You can do this through the Visual Studio 2019 NuGet package manager or by running the command dotnet add package System.ValueTuple in the .NET CLI.

Note that ValueTuple is available in .NET Framework 4.7.

How to Create a ValueTuple in C#?

There are multiple ways to create a ValueTuple: using a constructor, the Create method, or parentheses ().

Here’s an example of creating a ValueTuple using its constructor:

ValueTuple<int, string, bool> personInfo = new ValueTuple<int, string, bool>(25, "Alice", true);

You can also create a ValueTuple using the Create method.

var productInfo = ValueTuple.Create(101, "Laptop", 799.99);

You can assign member names and values to a ValueTuple like this:

var book = (Title: "C# Programming", Author: "John Doe", Year: 2023);

Alternatively, you can create and initialize a ValueTuple using parentheses, as shown in the following example:

(string Title, string Author, int Year) book = ("C# Programming", "John Doe", 2023);

How to use named properties in a ValueTuple in C#

You can use named properties in a ValueTuple to assign meaningful names instead of the default Item1, Item2, etc.

For example:

(double Latitude, double Longitude, string Location) coordinates = (40.7128, -74.0060, "New York");

How to return a ValueType from a method in C#

Similar to a Tuple, you can use a ValueTuple to return multiple values from a method.

static (int, string, string) GetProduct()
{
    return (ProductId: 101, ProductName: "Laptop", Category: "Electronics");
}

How to Retrieve individual members from a ValueTuple using deconstruction in C#

You can use deconstruction to retrieve individual members from a ValueTuple.

(int ProductId, string ProductName, double Price) = GetProduct();

Additionally, you can convert a ValueTuple to a Tuple using extension methods.

For example:

var valueTuple = ValueTuple.Create(101, "Laptop", 899.99);
var tuple = valueTuple.ToTuple();

ValueTuples offer a simpler syntax, improved performance, and the ability to modify their members and assign meaningful names, making them a superior choice over Tuples.