dcsimg

C# 7.0 Feature: Tuples In C#

WEBINAR:
On-Demand

Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


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

In .NET, developers used different ways to return multiple values from a method before Tuples were introduced. These are:

  • 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.

Installing System.ValueTuple
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.

Program output
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!



About the Author

Tapas Pal

I am working in Microsoft Technology for last 15 years and presently working with Cognizant Technology Solutions, India as Senior Architect. I have completed TOGAF 9.1,Microsoft Certification on .NET 1.1 , .NET 2.0, SQL Server 2005 and Sharepoint. Please visit my Blog - http://tapas-pal.blogspot.com/

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date