Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# 7.0 – Tuples Re-Engineered

0.00/5 (No votes)
23 Apr 2017 1  
C# 7.0 – Tuples Re-Engineered

Tuples have been around for quite some time as a convenient way of returning more than one value from a method. Even though Tuples were quite easy to use, from the code-readability perspective, there was certainly room for improvement. In C# 7.0, team Microsoft made a lot of enhancements with introduction of Tuple Types and Tuple Literals. Let’s review those changes. A typical use of Tuple could look as follows:

C#
static void Main(string[] args)
 {
    Tuple<int, string> info = GetInfo();
    Console.WriteLine($"{info.Item1} -> {info.Item2}");
 }

static Tuple<int, string> GetInfo()
 {
    return new Tuple<int, string>(1, "Joe");
 }

With the new syntax, the GetInfo method can be rewritten as follows. This is basically Tuple Literals where the data items are comma separated within the parenthesis. Also notice that the function return type is using the same syntax.

C#
static (int, string) GetInfo()
{
    return (1, "Joe");
}

On the caller side, you can still use the old way of calling the method.

C#
static void Main(string[] args)
{
    (int, string) info = GetInfo();
    Console.WriteLine($"{info.Item1} -> {info.Item2}");
}

Of course, Type inference could still be applied as well when calling the method.

C#
static void Main(string[] args)
{
    var info = GetInfo();
    Console.WriteLine($"{info.Item1} -> {info.Item2}");
}

As shown in the image below, the tool-tip is showing the appropriate type has been inferred.

1

However, the declaration of Tuple without any variable names doesn’t make code easily readable and understandable. The good news is that there is a better way. The code snippet below shows a newer version of GetInfo method. In this case, we have used the names of the variables which makes it much easier to understand as to what data is returned from this method.

C#
static (int id, string name) GetInfo()
{
   return (id:1, name:"Joe");
}

These variable names can also be used on the Caller side as shown below.

C#
static void Main(string[] args)
 {
   var info = GetInfo();
   Console.WriteLine($"{info.id} -> {info.name}");
 }

The type inference also shows these variable names as shown below:

2

When consuming the Tuples, there is an even better way by what’s called “deconstructing” a tuple. Code snipping below shows that the value returned by GetInfo method does not have a variable name but it's consumed in the Console.WriteLine method just the tuple variable names.

C#
static void Main(string[] args)
{
    (int id, string name) = GetInfo();
    Console.WriteLine($"{id} -> {name}");
}

For the var junkies out there, the code snippet above can be used as follows also:

C#
static void Main(string[] args)
{
   (var id, var name) = GetInfo();
   Console.WriteLine($"{id} -> {name}");
}

And to simplify it even further, you can do as follows just with one var.

C#
static void Main(string[] args)
{
   var ( id, name) = GetInfo();
   Console.WriteLine($"{id} -> {name}");
}

Until next, happy debugging!!

Filed under: .NET, CodeProject, Visual Studio 2017
Image 3 Image 4

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here