C#

Getting Excited About C# 7.0 Tuples

I’ve had the good luck to work on a number of legacy (read: profitable, stable) enterprise code bases. While appreciate all the benefits that such work entails, sometimes it means others may be getting into the new hotness, while my work is rather cool to the idea of adopting the latest platform developments.

But eventually, legacy systems do move into the future. So it is with a great deal of excitement that I’ve been finding my self reading and rereading half a year old spoilers i.e. language roadย blogs for C# 7.0.

https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

Tuples in particular are practically making me squeal with glee. Here’s the basic form:

public (string fullName, int id, bool isFaculty) GetRegistrant(string[] email)
{
  //Retrieve registrant info into local variables  
  return (fullName: fN, id: i, isFaculty: iF)
}

public static void Main()
{
    var registrant = GetRegistrant("test@example.com);
    Console.WriteLine($"Hello {registrant.fullName}!");
    var (name, number, occupation) = GetRegistrant("another@example.com";
    Console.WriteLine($"Your ID is {number}");
}

Check out the blog for more details, but here are the key features I enjoy.

Out variables are terrible. Anything that lets me use less of them is a good thing.

Custom return types are boilerplate. Removing ceremony while retaining type safety is amazing.

Tuples with unnamed item members are never self documenting code.
I’m tired of having to go back to the calling code to clarify if it was the first int or the second that was my ID.

These factors together make Tuples the changes I looking forward to most when my legacy code base in updated.

What’s your favorite feature of C# 7.0? Leave a comment below and tell me.