Tuples in C# 7 and Visual Studio 2017

Tuples in C# 7 and Visual Studio 2017

Tuples in C# 7 and Visual Studio 2017 received a much needed facelift. I, personally, find them just as enjoyable as tuples in Python given the brevity of the new syntax, optional naming of the tuple elements (which makes a world of difference in self-documenting code), and the ability to deconstruct tuples.

Below is a C# 7 method, called CalculateAreaOfCircle, that calculates the area of a circle given its radius. The method returns a tuple consisting of 2 named elements. The first element is the radius provided by the caller and the second element is the area of the circle.

class Program {
    static void Main(string[] args) {
        var circle = CalculateAreaOfCircle(2);
        Debug.WriteLine($"A circle of radius, {circle.radius}," +
            $" has an area of {circle.area:N2}.");
    }

    /// 
    /// Calculates the area of a circle given its radius.
    /// 
    ///The radius of the circle.
    /// Radius and area of the circle.
    static (int radius, double area) CalculateAreaOfCircle(int radius) {
        return (radius, Math.PI * Math.Pow(radius, 2));
    }
}

System.ValueTuple

First things first, if you create a new Visual Studio 2017 Console App in .NET Core to try tuples in C# 7, one must first add System.ValueTuple from Nuget to experience the new enhancements.

C# 7 Tuples with Named Elements

Notice the tuple returned by the CalculateAreaOfCircle method. The tuple incudes optional named elements of radius and area.

static (int radius, double area) CalculateAreaOfCircle(int radius)
{ ... }

circle = CalculateAreaOfCircle(2);
Debug.WriteLine($"radius = {circle.radius}, area = {circle.area}");

If you don't include the optional named elements, the individual tuple elements will be referred to as Item1 and Item2.

static (int, double) CalculateAreaOfCircle(int radius) { ... }

circle = CalculateAreaOfCircle(2);
Debug.WriteLine($"radius = {circle.Item1}, area = {circle.Item2}")

This is a big deal as the named elements make it much easier to understand the values that comprise the tuple returned by the method.

Tuple Deconstruction in C# 7

Another option in C# 7 is to deconstruct tuples using deconstructing declarations. This allows one to split apart the tuple into individual variables. One can collapse the declaration using a single var declaration.

var (radius, area) = CalculateAreaOfCircle(2);
Debug.WriteLine($"radius = {radius}, area = {area}");

One can also use multiple declarations to deconstuct the tuple.

(var radius, double area) = CalculateAreaOfCircle(2);
Debug.WriteLine($"radius = {radius}, area = {area}");

If variables already exist for the radius and area, just use those without declaring new variables.

(radius, area) = CalculateAreaOfCircle(2);

Conclusion

If you used tuples prior to C# 7, you know they are quite awkward. I'd say they were practically unusable. With the new tuple enhancements in C# 7, however, they are fantastic. I find them just as enjoyable as tuples in other languages, like Python.

Also read another C# Tutorial, C# 7 Local Functions in Visual Studio 2017.