C# Tuples Introduction

A c# tuple class is a data structure with a specific number of elements. A tuple is used to store elements which have related behavior like by using tuple we can store person name in the first position, date of birth in the second,…., etc. We can also store database record in the tuple. In this article, we discuss what are tuples and in C# and its advantages.

Open Microsoft Visual Studio 2015 => Create console application and name it as CSharpTupleExample.

namespace CSharpTupleExample

{

    class Program

    {

        static void Main(string[] args)

        {

            var personInfo = new Tuple<string, string, DateTime>("Raj","Houston",Convert.ToDateTime("01/01/1990"));

        }

    }

}

As shown above we are storing person information in a tuple object personInfo. Tuple object contains name in first position, location in second position, and date of birth in third position. We can also use tuple helper method Create() to create the tuple object as shown below.

class Program

 {

        static void Main(string[] args)

        {

            var personInfo = Tuple.Create("Raj","Houston",Convert.ToDateTime("01/01/1990"));

        }  

 }

We can have tuple object with any number of components, but by using helper method create() we can create tuple object which can have one to eight components. If you want to create object with more than eight components use first method. As shown below we can create tuple with different number of components.

var tupleObj = Tuple.Create(component1);

var tupleObj = Tuple.Create(component1, component2);

var tupleObj = Tuple.Create(component1, component2, component3);

var tupleObj = Tuple.Create(component1, component2, component3, component4);

var tupleObj = Tuple.Create(component1, component2, component3, component4, component5);

var tupleObj = Tuple.Create(component1, component2, component3, component4, component5, component6);

var tupleObj = Tuple.Create(component1, component2, component3, component4, component5, component6, component7);

var tupleObj = Tuple.Create(component1, component2, component3, component4, component5, component6, component7, component8

We can create tuple object with more than eight components as shown below.

var tupleObj = new Tuple<anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype>

                                (component1, component2, component3, component4, component5, component6, component7, component8, component9);

var tupleObj = new Tuple<anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype, anyDatatype,....>(component1, component2, component3, component4, component5, component6, component7, component8, component9, component10,......); 

Tuple object element can be any data type. We can access tuple object elements like normal object properties as shown below.

using System; 

namespace CSharpTupleExample

{

    class Program

    {

        static void Main(string[] args)

        {

            var personInfo = Tuple.Create("Raj","Houston",Convert.ToDateTime("01/01/1990")); 

            Console.WriteLine("Name: " + personInfo.Item1);

            Console.WriteLine("Location: " + personInfo.Item2);

            Console.WriteLine("Date Of Birth: " + personInfo.Item3.Date);

            Console.ReadLine();

        }

    }

} 

Tuple Advantages:

  1. We can use tuple object when we need to represent a single set of data as a database record.

  2. When we need to return multiple values from a C# method we can use tuple object instead of out parameter.

  3. To pass multiple values as a single parameter to a method, we can use tuple object by adding multiple values to tuple object.