Search⌘ K

Tuples

Explore the concept of tuples in C#, introduced in version 7.0, to group multiple values into a single unit. Learn how to create tuples, access elements by position or name, and use tuples to return multiple results from methods. This lesson helps you write cleaner code when handling related data values together.

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:

 ...