Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Inferred Tuple Names in C# 7.1

DZone's Guide to

Inferred Tuple Names in C# 7.1

Let's take a look at inferred tuple names, a new feature in C# 7.1, why it's useful, and how to make use of them without losing code readability.

· Web Dev Zone ·
Free Resource

Learn how Crafter’s Git-based content management system is reinventing modern digital experiences. Download this white paper now. 

Although C# 7.1 doesn't have many new features, there are still some convenience hacks I like. One of these is inferred tuple names, meaning that we can name tuple members using variable names. It’s not a big change in the language, but it still makes code a little bit cleaner where tuples are used.

New Features in C# 7.1

  1. Switching C# version in Visual Studio
  2. Deep dive to async Main
  3. Inferred tuple names in C# 7.1

In C# 7.0 we had to specify the names of value tuple members like this:

public static void Demo(){    
  var count = 5;    
  var type = "Orange";     
  var tuple = (Count: count, Type: type);
}

Without this, the names will be default ones (Item1,…,ItemN).

In C# 7.1 we don’t have to specify member names anymore if we are using variables to define a tuple:

public static void Demo(){    
  var count = 5;    
  var type = "Orange";     
  var tuple = (count, type);
}

As a result, our tuple has two members with values names as count and type.

Casing issue: Usually, our local variables that we use to build tuples are all in lower case. If we want tuple member names to be in upper case, we still have to specify names for tuple members.

Wrapping Up

Inferring tuple member names is actually a convenient feature, but it doesn’t (yet) support correct casing we have used within C#. It can be an issue for perfectionists, but others can still go with variable-based tuple member names without losing readability of code.

Crafter CMS is a modern Git-based platform for building innovative websites and content-rich digital experiences. Download this white paper now.

Topics:
c# ,tuples ,web dev ,programming

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}