C# 7.0 Feature: Tuples In C#
Introduction
A Tuple is a kind of ordered sequence of heterogeneous objects. Items in that sequence are either different or may be of the same type. A method could return multiple homogeneous values. To return multiple homogeneous values, it stores these values in the collection and returns that collection. But, if a method needs to return multiple heterogeneous values, C# 7.0 provides Tuples. Tuples are designed to be very lightweight data structures that contain multiple fields to represent the data members.
Tuple: Technical Details
A Tuple is a reference type. To use Tuple in the code, the syntax was verbose and also with System.Tuple, being a reference type, the possible null cases needs to be handled additionally. Tuples enable developers to package multiple values in that single object more easily.
Before Tuples
- Out parameters
- Using arrays
- Anonymous types
Tuple has solved the long-pending request from developers to have a lightweight mechanism to return multiple values from any method as the existing option.
Sample Application with Tuple
Let's explore Tuple by directly creating a simple example where we have a method which will return The Sum and Count of an Array that we are passing to it. Open Visual Studio -> On the menu bar -> Choose File -> New -> Project -> Expand Installed -> Expand Templates -> Expand Visual C# -> and then choose Console Application and name it 'PrjSampleTuple'. To use Tuple in the application, we need to install System.ValueTuple from a NuGet package. This is shown in Figure 1.
Figure 1: Installing System.ValueTuple
I have created a simple example where I have a method which will return the Sum and Count of an Array that we are passing to that method. Following is the code snippet of the method:
private static Tuple<int, double> CountingandSummation(IEnumerable<double> numbers) { int count = 0; double sum = 0.0; foreach (var value in numbers) { count++; sum += value; } return Tuple.Create(count, sum); }
Next, I have called the method from main (), as the following code snippet explains.
static void Main(string[] args) { IEnumerable<double> numbers = new double[] { 15, 200, 40, 60, 80, 90 }; var t = CountingandSummation(numbers); Console.WriteLine($"Sum of the {t.Item1} items are {t.Item2}"); Console.ReadLine(); }
As we understand, whenever we return more than 1 value using Tuples, we access them in the called method using their item number. The final code snippet of the program follows.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PrjSampleTuple { class Program { private static Tuple<int, double> CountingandSummation(IEnumerable<double> numbers) { int count = 0; double sum = 0.0; foreach (var value in numbers) { count++; sum += value; } return Tuple.Create(count, sum); } static void Main(string[] args) { IEnumerable<double> numbers = new double[] { 15, 200, 40, 60, 80, 90 }; var t = CountingandSummation(numbers); Console.WriteLine($"Sum of the {t.Item1} items are {t.Item2}"); Console.ReadLine(); } } }
Figure 2 shows the output of the preceding program.
Figure 2: Program output
Conclusion
Tuple has some limitations; for example, a Tuple instance has a fixed number of items. If we want to create a Tuple with more items, we have to create nested Tuples.
I hope this article has provided you with basic information about C# Tuples and how developers can use Tuples in code.
That's all for today. Happy reading!