Tuples in C#

A tuple is a data structure that is used to represent finite sequence of elements (a set of data). Elements in this set of data may be of different types. Tuples in C# can be used to:

  • To create and access a set of data
  • To manipulate a set of data
  • Return multiple values from a method without using out parameters.
  • Pass multiple values to a method using single parameter

Let’s see each of the functionality in details using examples.

Create and access Tuples in C#

A tuple is generally referred as n-tuple, where n is the elements in the tuple. For example, if a tuple is having three elements then it will be known as 3-tuple.

Use new keyword and pass set of values in constructor to create a Tuple<> object.  Values in tuple can be accessed using item number. In the below example, I am creating a 3-tuple that represents an employee’s name, gender and age. Here the two of the data types are string but one is integer.

Result:

Tuples in C# - 1

We can also create tuples using “Create” static method of Tuple class. Below given example creates 4-tuple that represents employee’s ID, name, gender and age.

Result:

Tuples in C# - 2

Tuples as a parameter

We can pass a data set using tuple as single parameter to a method. No need of ref or out keyword. Below code sample passes a 3-tuple as a parameter to a method.

Result:

Tuples in C# - 3

Return Tuples

Tuple allows to return a set of data (multiple values) using one tuple object. No need for ref or out keyword.

Nested Tuples

Tuples can be nested inside another tuple.

The .NET framework supports tuples from one to seven elements. But, you can create tuples of eight or more elements by nesting tuple objects using TRest property of Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object.

Example:

Result:

Tuples in C# - 5

Tuples in C# 7.0

There are multiple new features available for tuples in C# 7.0, like tuple return types, tuple literals, inline tuples and tuple deconstruction. We will these features one by one.

First thing, if C# 7.0 feature in not available with Visual studio then first install System.ValueTuple NuGet package.

Tuples in C# - 4

Tuple type and tuple literals

You can write tuple return type in small parenthesis. Let’s rewrite the code that we used above using C# 7.0.

 

In previous section, we discussed that tuple items can be accessed suing item number. This is a problem because you need to remember the sequence in order to retrieve the correct item. In C# 7.0, Tuple types resolves this problem, which makes the code much readable.

Tuple literals are used to create instance of tuples. The items inside a tuple can be accessed as a variable.

You can also use already defined variable to create instance of a tuple.

Var keyword can be used to store tuple variables.

Name your tuple items for better readability.

Tuple deconstruction

Tuple deconstruction is nothing but to assign a returned tuple type to a new variable and use the values as needed.

Hope you get an idea bout tuples in C#. Post your comments for queries and feedback. Thanks for reading.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Leave a Reply

Your email address will not be published. Required fields are marked *