How to use ValueTuples in C#

Take advantage of ValueTuples to return multiple values from a method with better performance than Tuples

How to use ValueTuples in C#
Thinkstock

A Tuple is a data structure that comprises an ordered, finite sequence of immutable, heterogeneous elements of fixed sizes. When we say the elements in a Tuple are immutable, we mean that they pertain to a specific type that cannot be changed.

A ValueTuple is a structure introduced in C# 7.0. A ValueTuple overcomes two major limitations of tuples—namely, that they are inefficient and that they must be referenced as Item1, Item2, etc. That is, ValueTuples are both performant and referenceable by names the programmer chooses. This article discusses what ValueTuples are and how we can use them in C#. 

To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here

Create a console application project in Visual Studio

First off, let’s create a .NET Core Console Application project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core Console Application project in Visual Studio 2019.

To continue reading this article register now