How to use ValueTuple in C#

by Kenji Elzerman
How to use ValueTuple in C# - Kens Learning Curve

Introduced in 2017 with C# 7.0 (.NET Framework 4.7), ValueTuple in C# provides a convenient and easy way to handle multiple values. It has gained a lot of popularity ever since. The idea behind a ValueTuple in C#is pretty simple: It’s a container that can hold multiple values without needing to create a class with properties. In this tutorial more about ValueTuple in C#.

The basics

The best way to explain a ValueTuple is to show it. In the code example below I defined a variable movie and look how I assigned a value to it.

var movie = (Title: "The Matrix", Rating: 5);

The assigned value looks a bit different than usual, but this is the ValueTuple in C#. I defined the properties (without a type) and a value. Somehow it makes me think of JSON.

I used var to define the data type of the variable movie, but you can also explicitly declare the variable. It would look like this:

(string Title, int Rating) movie = (Title: "The Matrix", Rating: 5);

It shows the properties with the data types. It’s a lot more typing, but it can be easier to read. Which one is better is personal and can be up for debate.

Now let’s take a look at how we can use the values in a ValueTuple. We can write the contents to a console like this:

(string Title, int Rating) movie = (Title: "The Matrix", Rating: 5);

Console.WriteLine(movie);

This will show the whole tuple in the console:

valuetuple result - ValueTuple in C# - Kens Learning Curve

Although it looks cool it’s not something you can work with. We can use the properties of a ValueTuple like a normal class.

(string Title, int Rating) movie = (Title: "The Matrix", Rating: 5);

Console.WriteLine($"This movie is {movie.Title} and has a rating of {movie.Rating}");

And one of the best features is that IntelliSense knows the properties.

ValueTuple in C# as return type

Maybe you already guessed it, but you can also use the ValueTuple as a return type, making a method return a set of data. The big advantage is that you don’t have to create classes that hold the properties of all the data you want to return. Best practice says that you don’t want to create ValueTuple in C# with 4 or more properties. The reason is that it will become unreadable. 

Take a look at the following code.

Movie foundMovie = GetMovie();
Console.Write($"The movie {foundMovie.Title} has a rating of {foundMovie.Rating}");

static Movie GetMovie()
{
    return new Movie
    {
        Rating = 5,
        Title = "The Matrix"
    };
}

class Movie
{
    public string? Title { get; set; }
    public int Rating { get; set; }
}

There is a method called GetMovie(), which returns an object of the type Movie. This is a class, on line 13, that holds two properties.

Now, let’s rewrite this with a ValueTuple.

var foundMovie = GetMovie();
Console.Write($"The movie {foundMovie.Title} has a rating of {foundMovie.Rating}");

static (string Title, int Rating) GetMovie()
{
    return (Title: "The Matrix", Rating: 5);
}

Way shorter, right? It went from 17 lines to just 7 lines of code.

Reasons to use ValueTyple in C#

It’s pretty lightweight since you don’t have to create a new class and reserve memory for it. ValueTuples can also increase performance because they are value types. And because they can remove the need for custom types and can be used with a few properties, ValueTuples can increase the readability of your code, especially with pattern matching.

Another reason to use ValueTuple in C# is they have a great integration with anonymous types. They also reduce the boilerplate because they are simpler and faster.

Reasons not to use ValueTuple in C#

It feels and looks cool, but there are some downsides. The most important one is readability. In the examples, I used two properties. From experience, I wouldn’t use more than that. The more properties you use, the less the readability will be.

The second item I would address is the duplicate code. Let’s take the last example as …. example. Say I create another class that returns the same properties. I could use the ValueTuple again. But what happens if I want to include the release date of the movies? I would have to add the property ReleaseDate to both methods. Better would be to create an object called Movie and use that one instead of the ValueTuple.

And the last reason why you shouldn’t use ValueTuple in C# is when the C# version is below 7. This seems logical since I noted the introduction date of this data type at the beginning of this tutorial.

Conclusion

For me, the ValueTuple in C#is something I never used before. Most of the companies I worked for never use(d) them. Since I learned more about this type I use it more and more. Especially for smaller projects.

It can save you a lot of code, as I have demonstrated. But it could also cause frustration if it’s used incorrectly. Using too many properties or using it for the same reason in different locations could make your code harder to read or understand.

Table Of Contents
Kens Learning Curve