Click here to Skip to main content
13,861,547 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.8K views
5 bookmarked
Posted 23 Apr 2017
Licenced CPOL

C# 7.0 – Tuples Re-Engineered

, 23 Apr 2017
Rate this:
Please Sign up or sign in to vote.
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:

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.

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

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

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.

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.

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.

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.

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:

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.

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

Until next, happy debugging!!

Filed under: .NET, CodeProject, Visual Studio 2017

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kamran Bilgrami
Architect
Canada Canada
Kamran Bilgrami is a seasoned software developer with background in designing mission critical applications for carrier grade telecom networks. More recently he is involved in design & development of real-time biometric based security solutions. His areas of interest include .NET, software security, mathematical modeling and patterns.

He blogs regularly at http://WindowsDebugging.Wordpress.com

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 83266444-May-17 12:37
memberMember 83266444-May-17 12:37 
GeneralSomething wrong with formatting of your text Pin
RenniePet25-Apr-17 18:53
memberRenniePet25-Apr-17 18:53 
GeneralMy vote of 5 Pin
Touristas23-Apr-17 23:32
memberTouristas23-Apr-17 23:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 24 Apr 2017
Article Copyright 2017 by Kamran Bilgrami
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid