Understanding C# 7 ValueTuples

One of the big new features introduced with C# 7 is the ValueTuple.
Now we all know Tuples, we heard of their existence in C#, saw them in a tutorial and decided not to use them, ever again because they were completely awkward and difficult to use.

So let’s remind ourselves what Tuples are and how they work prior to this great upgrade.

Let’s say we have a method which is trying to parse a string to an integer. That method will try to do the parsing and return it, but should also return if it was successful or not.
Now let’s ignore the Try pattern for now (this will actually be explained in a later blog post, as it still used by Microsoft in many of their libraries).

Now let’s examine the possible signature of that method? How would you return two results from a single method?
Well we could use out variables or reference variables. But we want to “explicitly return” more than one variable.

Well one could try using a struct or a class that contains the resulting values.
In our example we would have the following

But why create a class for a simple task that will happen only once? If this class was going to used in many places, or would have much information then maybe it would be useful. But we could come up with a better solution right?

Well here comes .Net 4 bringing along the System.Tuple type

So how do we use System.Tuple?
Well let’s start with our new Parse method.

Not much has changed here, instead of returning a class Results type, we return this Tuple thing and use it similary right?
Well kind of, in the following code we will see the usage and the awkward parts that came with the Tuple type.

See the issue here?
System.Tuple never had named elements. Which is a major pain in the ass if you ask me. You always have to backtrack and see which element is item1 and which is item2.

Another major issue with System.Tuple is the fact that it is a reference type. So you had to pay the penalty of heap allocation and increased pressure on the garbage collector.

ValueTuple to the rescue

C# 7 brought about the ValueTuple that solved the aforementioned issues.
It’s worth mentioning thought that if you are not targetting .net 4.7 you would need to download the following NuGet package in order to use them.

In order to write a method that has multiple return types we do not have to change much

But the real benefit comes when we do this

Now isn’t his handy? our return types are named and defined clearly for every single usage.

How to use that method you ask? Well simple

See how easy it is to use the values inside the Tuple? No need to remember which item comes first which second etc. But the real power comes with the fact that you can actually use them as multiple return values instead of inside a tuple, for example

And finally we have the multiple return values we so much craved for.
and let’s not forget, these are Value Tuples, which means they are regular value types. No heap allocation and de-allocation to worry about, no garbage collection, nothing.
That means that we can actually use the ValueTuple in performance critical code without any worries.

Please follow and like us:

Leave a Reply