Tuple In C# 7.0


This article introduces new features of tuple in C# 7. When we want to return multiple values from a method then there are following options.

  1. The method returns an object which has multiple properties.
  2. The method returns single values and has out and ref parameters.
  3. A method returns an array or collection of data such as List These are homogeneous objects and values.
  4. A method returns a Tuple. It is a lightweight data structure of heterogeneous objects and values.

Let’s create an example. This example intends to find out the minimum and maximum number from a collection of integer values. It holds a method which returns both Minimum and Maximum values. We have three options to accomplish this task. These options are out parameter, Class/Struct, and Tuple. So, let’s see each option one by one.

You may like to read

  1. Ref Return and Ref Local In C#
  2. Pattern Matching In C#
  3. Expression Bodied Members In C#

Using Out Parameter


Let’s create an example. This example holds a method which intends to find out the minimum and maximum number from a collection of integer values. It uses out parameter to returns minimum value and returns maximum value.

Now we create a class named Operation which holds a method named FindMinMax to find a minimum and maximum number from number series. The following code snippet for same.

As per the preceding example, the method returns a value which holds in a variable named maximum. That value is a maximum number from series. This method also takes out parameter as an argument named minimumNumber which holds a minimum value from series.

Now, Create an executable program which uses as application interface. This program calls FindMinMax method of Operation class.The following code snippet for same.

Now, run the application and result shows as the below output figure.

Output 1: Max using out

The preceding example works perfectly with out parameter but out parameter doesn’t work with async method.

Using Class/Struct


As we have used out parameter to return multiple values from a method. Similarly, we have an another option to get multiple values from a method which is Class or Struct with multiple properties. We rewrite the same functionality to find out the minimum and maximum numbers from a number series using Struct. Let’s create a struct named CalculateData which holds two public properties for the minimum and maximum numbers from a series. The following code snippet for same.

The data structure is ready. Now, we create an another class named Operation to perform operations. It holds a method to find the minimum and maximum numbers from number series. The following code same.

As per the preceding example, the method returns a struct object which properties hold the maximum and minimum number from series.

Now, we use the same executable program and update it. This program calls FindMinMax method of Operation class.The following code snippet for same.

Now, run the application and result shows as the below output figure.

Output 2: Max and Min from number series

Using Tuple


The Tuple is already available in C#. Its features are enhanced C# 7.0. As Tuple holds multiple heterogeneous values so it’s one more option to get multiple values from a method. It is available through NuGet package. So, we install System.ValueTuple NuGet package in the application.

Figure 1: NuGet Package for Tuple

We perform the same previous operation using Tuple. We update FindMinMax method under class Operation. This method finds the minimum and maximum number and returns both numbers using a tuple. The following code snippet is used for the same operation.

As per the preceding example, the method FindMinMax returns a tuple. It has two items which hold the maximum and minimum number from series.

Now, we use the same executable program and update it. This program calls FindMinMax method of Operation class. The following code snippet for same.

As per the preceding example, the method named FindMinMax calls in executable program. When we get more than one values from a tuple type then it gives default names as per these order such as item1, item2 etc. Tupple items are used in the same order in which they are created.

Now, run the application and result shows as the below output figure.

Output 3: Maximum and Minimum number using Tuple

As per preceding example, it uses Item1 and Item2 for tuple values. These don’t represent actual names of fields. C# 7 provides features that these items can be assigned custom names rather than Item1 and Item2.

Now, Let’s update FindMinMax method under the Operation class. In the following code snippet, we assign custom names to tuple fields.

As per the preceding code, the tuple has two fields which are Minimum and Maximum respectively.

Now, we use the same executable program and update it. This program calls FindMinMax method of Operation class. The following code snippet for same.

As per the preceding example, the method named FindMinMax calls in executable program. As this method returns a tuple which has two items Minimum and Maximum. The values of these items are as per the sequence of items in the tuple.

Now, run the application and result shows as the below output figure.

Output 4: Minimum and Maximum using Tuple

Conclusion


A tuple uses when a method requires returning more than one values of heterogeneous objects. Tuple has a 8 items limit but it has an option to nested tuple. The last item of a Tuple can have another Tuple.