C# 7 Tuple Improvement


As you all know, there are a number of ways to return more than one data type from a function in C#. I am going to explain how to return multiple values from a C# function using a Tuple.

Before I jump into Tuple improvement, let's try to understand how this works prior to C# 7.0 with a small example.

Let us create a class named Calculator. This class has got one method named GetAritmenticOpeartionsPriorCsharp7 which accepts two integer parameters. This function will return the sum and multiplied values of the supplied inputs.

The function GetAritmenticOpeartionsPriorCsharp7 returns two integer values. The first value is the sum of inputs and the second value is the multiplication of supplied inputs.

Now let's call this method from a console application, as shown below.

Let's run this application and find out the output.

Everything worked as expected. One problem with this approach is that the tuple values were accessed as Item1 and Item2. The consumer has no idea whether Item1 represents Sum and Item2 represents Product. The developer must remember which parameters belong to what. It would have been great if we could have renamed Item1 as Sum and Item2 as Product.

Luckily C# 7 supports this. Let's see in action.

Let's create the same Calculator class using C# 7 syntax. Create a static method named GetAritmenticOpeartionsInCsharp7, as shown below.

In this approach, I have explicitly mapped the addition of 2 numbers to variable ‘Sum’ and multiplied results in variable ‘Product’.

Now let's rewrite the console application code as shown below.

Now from the output variable, I can determine which is the 'Sum' and 'Product' output. The application output is shown below.

Everything worked as expected. I hope you all enjoyed it.


This article talks about some of the new changes introduced in C# 7.0 regarding Tuples.

Source: Paper.li