Tuples

Tuples are a convenient way to pack several values into one variable.

We'll cover the following

Syntax

Tuples are a relatively new concept in C#. They first appeared in C# 7.0. Tuples provide a convenient way to work with several values.

A tuple is a set of values enclosed in parentheses:

(int, int) someTuple = (4, 2);

Here, we create a variable of type (int, int), which is a tuple of two int values. The order in which values are placed matters. We could declare the type implicitly by using the var keyword:

var someTuple = (4, 2);

The var keyword instructs the compiler to determine the type for us by looking at the right-hand side of the assignment operator. In other words, it’s impossible to use the var keyword to create a variable without assigning it a value:

var someTuple; // Not allowed

Get hands-on with 1400+ tech skills courses.