Save Your C# Keystrokes With Tuples
Tags: dotnet
Published: 2020-02-21 02:42:51

---
title: Save Your C# Keystrokes With Tuples
published: false
date: 2020-02-11 00:00:00 UTC
tags: dotnet
canonical_url: https://khalidabuhakmeh.com/save-your-csharp-keystrokes-with-tuples
published: true
---

In the year 2000, Microsoft released C# to the masses, and since then, it has evolved with many new features. One of those features includes Tuples. Only recently have tuples become more useful with the addition deconstruction in C# 8. In this post, I’ll show you how to save your keystrokes by working with Tuples. We’ll look at two identical code snippets with as much readability, but significantly fewer characters used.

<!--more-->
## What Is A Tuple

A Tuple is a newer data structure introduced in later versions of the .NET Framework. Microsoft’s documentation describes a Tuple as:

> A tuple is a data structure that has a specific number and sequence of elements.<cite>–<a href="https://docs.microsoft.com/en-us/dotnet/api/system.tuple?view=netframework-4.8">Microsoft</a></cite>

It’s likely easier to understand what a Tuple is by looking at an example.

```c#
// Microsoft Documentation Example
var primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19); 
Console.WriteLine("Prime numbers less than 20: " + 
    "{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7}", 
    primes.Item1, primes.Item2, primes.Item3, primes.Item4, 
    primes.Item5, primes.Item6, primes.Item7, primes.Rest.Item1); 
// The example displays the following output: 
// Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19

```

Tuples are limited to a structure containing seven items. That said, if you’re using Tuples with this many items, you may want to use a `class`.

## Deconstruction To Save Keystrokes

Let’s take a look at an example using an anonymous `friend` object collection. Each friend has a `name`, `nickname`, and a set of `hobbies`.

```c#
var friends = new[]
{
    new
    {
        name = "Bruce Wayne",
        nickname = "Batman",
        hobbies = new[] {"dressing up", "crime-fighting", "parkour"}
    },
    new
    {
        name = "Natalia Romanova",
        nickname = "Black Widow",
        hobbies = new[] {"martial arts", "spying", "guns"}
    },
    new
    {
        name = "Arthur Curry", 
        nickname = "Aquaman", 
        hobbies = new[] {"surfing", "conversations", "fish"}
    }
};

foreach (var friend in friends)
{
    Console.WriteLine($"{friend.name} ({friend.nickname}) likes {string.Join(", ",friend.hobbies)}.");
}

```

When we execute the code, we get the following output:

```console
Bruce Wayne (Batman) likes dressing up, crime-fighting, parkour.
Natalia Romanova (Black Widow) likes martial arts, spying, guns.
Arthur Curry (Aquaman) likes surfing, conversations, fish.

```

The character count of the previous code is **606** characters.

Let’s take a look at how we can use Tuple deconstruction to shorten that code.

```c#
var friends = new[]
{
    (
        "Bruce Wayne",
        "Batman",
        new[] {"dressing up", "crime-fighting", "parkour"}
    ),
    (
        "Natalia Romanova",
        "Black Widow",
        new[] {"martial arts", "spying", "guns"}
    ),
    (
        "Arthur Curry",
        "Aquaman",
        new[] {"surfing", "conversations", "fish"}
    )
};

foreach (var (name, nickname, hobbies) in friends)
{
    Console.WriteLine($"{name} ({nickname}) likes {string.Join(", ", hobbies)}.");
}

```

Executing the tuple deconstruction code, we get the following output.

```console
Bruce Wayne (Batman) likes dressing up, crime-fighting, parkour.
Natalia Romanova (Black Widow) likes martial arts, spying, guns.
Arthur Curry (Aquaman) likes surfing, conversations, fish.

```

No surprise, it is identical to the first code execution. The difference being that this codebase has a character count of **497**. That’s an **18%** reduction in keystrokes. The savings come from not having to define property names, and the use of `.` notation to access those same properties.

> By using Tuples we reduced our character count by 18%!<cite>–Khalid</cite>

## Conclusion

Should you switch your entire codebase to use tuples? You should not. I think the advantage of tuples presents itself in tightly scoped situations. These situations can include scripts, unit tests, and code samples. In my opinion, tuples break down once you begin to pass them to external consumers. The items comprising my tuples are clear in the sample code, but if I were to return the tuple to an ignorant consumer, the intention of the data structure would be lost. **Like all new things, take the time to absorb the idea, evaluate its value, and use it accordingly.** I hope you found this blog post educational. Thank you for reading.