X

Inferred tuple names in C# 7.1

Although C# 7.1 hasn’t 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 language but it still makes code a little bit cleaner where tuples are used.

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 tuple:

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

    var tuple = (count, type);
}

As a result out 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 convenient feature but it doesn’t support (yet?) correct casing we have used with in C#. It can be issue for perfectionists but others can still go with variable based tuple member names without losing in readability of code.

Liked this post? Empower your friends by sharing it!
Categories: .NET C#

View Comments (0)

Related Post