Value tuples is one of the features added in C# 7. I’m going to show you why it was introduced to language and how you can use it.

Tuple

Before we start with value tuples let’s take a look at regular tuples. What’s a Tuple? Basically it’s a set of grouped properties.

You can use it to:

  • quickly group related variables
  • pass multiple related values as a single parameter
  • return multiple values from method

General characteristics of a Tuple:

  • reference type
  • generic, each property has its own type
  • can store up to 8 different members, you can use nesting for more
  • access to its properties in possible by properties named Item1, Item2 and so on

Let’s see how can we use it in practice.

1
2
3
4
5
Tuple<string, DateTime> person 
        = Tuple.Create("John", new DateTime(2000, 1, 1));

var name = person.Item1;
var birth = person.Item2;

It looks rather easy, but the naming of properties is rather uncomfortable, it can become hard to recognize (and remember) what’s in each property. It can also get a little long if you use tuples with more properties.

ValueTuple

The use cases for Tuple and ValueTuple are basically the same. There are however some differences, value tuples are:

  • value type
  • generic, each property has its own type
  • can store up to 8 different members, you can use nesting for more
  • simplified syntax
  • you can give names to properties

Let’s see how previous example would look like with ValueTuple.

1
2
3
4
(string, DateTime) person = ("John", new DateTime(2000, 1, 1));

var name = person.Item1;
var birth = person.Item2;

Same example, but with named members:

1
2
3
4
(string name, DateTime birth) person = ("John", new DateTime(2000, 1, 1));

var name = person.name;
var birth = person.birth;

It’s worth mentioning that the names are design time only, during runtime the names are like Item1, Item2 and so on.

You can make it even shorter by using deconstruction. It means that we can assign values directly to variables. Take a look at the source code below:

1
2
3
4
// this line initializes variables with following values
// (string) name = "John"
// (DateTime) birth = new DateTime(2000, 1, 1)
var (name, birth) = ("John", new DateTime(2000, 1, 1));

C# 7 deconstruction is not limited to ValueTuple. You can deconstruct any type with Deconstruct method. Here’s an simple example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Color
{
    public int R { get; set; }
    public int G { get; set; }
    public int B { get; set; }

    public Color(int r, int g, int b)
    {
        this.R = r;
        this.G = g;
        this.B = b;
    }

    public void Deconstruct(out int r, out int g, out int b)
    {
        r = this.R;
        g = this.G;
        b = this.B;
    }
}

// deconstruction
var (r, g, b) = new Color(255, 255, 255);

If you don’t need all values then you can use discards, which skips the value in deconstruction process.

1
2
// discard g and b, we're only interested in r
var (r, _, _) = new Color(255, 255, 255);

You can also create deconstruction for types wihich you didn’t author using extensions method. Whole process is described on
Deconstructing tuples and other types
.

Summary

Value tuple is and interesing addition. It certianly makes code less verbose, the syntax is simplified and short.