Overview Of Tuples In C#

What is Tuple and when it was introduced?


A Tuple is a new data type introduced from .Net Framework 4.0. When there is a requirement to store multiple values of different data types, then we can go with Tuple. Simply “Tuple is a container of values with similar or different data type which supports 7 items (similar/different data types) plus 1 Tuple type”. Point to be noted here is, it is not a collection.  

When to use Tuple?

Tuple can use to store any single set of data. We have few specific scenarios where only a Tuple can serve.

Scenario1

Generally, a method can return at most two values including out parameter. When we want to return more than two values without having to create any separate class, then we can use Tuple and Vice versa.

Scenario2

We have a requirement to fetch a single row from the database every time and need to handle data without more efforts. This situation can manage with Tuple by assigning values to Tuple.

How to declare a tuple?

We can declare a Tuple as follows. 

  1. //Single valued Tuple   
  2. Tuple<int> TupleObjectName;  
  3. //Double valued Tuple  
  4. Tuple<int,string> TupleObjectName;  

Syntax

Tuple<T1>
Tuple<T1, T2>
Tuple<T1, T2, T3>
Tuple<T1, T2, T3, T4>
Tuple<T1, T2, T3, T4, T5>
Tuple<T1, T2, T3, T4, T5, T6>
Tuple<T1, T2, T3, T4, T5, T6, T7>
Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>

Here, T can be any type and TRest is a Tuple type.

If you have seen it, while declaring a tuple, you will get 8 override functions, as shown below.


In the last override method, we can add 7 types with one Tuple type (I will explain in detail with an example). 

Assigning a value to tuple. 

Let’s assume, we have declared a tuple as follows.

  1. Tuple<intintintdouble> ArithmeticResult;  
Now, we can assign a value to it in two ways.

Method 1

  1. ArithmeticResult = new Tuple<intintintdouble> (1, 1, 2, 1.11);  

Method 2

  1. ArithmeticResult = Tuple.Create(1, 1, 1, 2.2);  

Tuple.create() vs new Tuple

Primarily, there is no difference in both. We use both to set the value to Tuple but when we are working with TRest(Tuple) item as the 8th data type, Tuple.Create() won’t works as we not mentioning the data types here.

See the following example . 

  1. //Tuple with 8 items with one Tuple type  
  2. Tuple<intintintintintintint, Tuple<intint>> dataWithEightarguments;  
  3. //Preparing a Tuple for 8th item  
  4. Tuple<intint> innerData = Tuple.Create(2, 3);  
  5.   
  6. //Assigning value with New Tuple Syntax-- No error  
  7. dataWithEightarguments = new Tuple<intintintintintintint, Tuple<intint>>(2, 2, 2, 2, 2, 2, 2, innerData);  
  8.   
  9. //Assigning value with Tuple.Create() -- Will get Compilation error  
  10. dataWithEightarguments = Tuple.Create(1, 1, 1, 1, 1, 1, 1, innerData);   
Let’s understand Tuple with an example

Assume that, we have to return all arithmetic results (+,-,*, /) of two integers from a method at one shot. To achieve the requirement I will create a method with tuple return type as shown below.

  1. public Tuple < intintintdouble > getArithmeticValues(int a, int b)  
  2. {  
  3.     Tuple < intintintdouble > ArithmeticResult;  
  4.     ArithmeticResult = new Tuple < intintintdouble > (a + b, a - b, a * b, (double) a / b);  
  5.     return ArithmeticResult;  
  6. }   

The method given above will return all the arithmetic results of the provided values (a, b).

How to read tuple

Once you have added values into tuple, it is easy to retrieve the data from it. Tuple stores the data in “Item” property. From the example given above, we can get the values, as shown below.

  1. Tuple < intintintdouble > result = p.getArithmeticValues(a, b);  
  2. Console.WriteLine("Addition: " + result.Item1);  
  3. Console.WriteLine("Subtractions: " + result.Item2);  
  4. Console.WriteLine("Multiplication: " + result.Item3);  
  5. Console.WriteLine("Division: " + result.Item4);   
A Tuple with more than 7 arguments.

Observe the following example. We have a Tuple called dataWithEightarguments with 8 parameters, in that 8th one is a Tuple type. So, while assigning data to it, we need to pass Tuple type data as the 8th parameter.

  1. public void StoreMoreThanSevenItemsinTuple() {  
  2.     Tuple < intintintintintintint, Tuple < intint >> dataWithEightarguments;  
  3.     Tuple < intint > innerData = Tuple.Create(2, 3);  
  4.     dataWithEightarguments = new Tuple < intintintintintintint, Tuple < intint >> (2, 2, 2, 2, 2, 2, 2, innerData);  
  5.     var innerDataReading = dataWithEightarguments.Rest;  
  6. }  
Ebook Download
View all
Learn
View all