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.
-
- Tuple<int> TupleObjectName;
-
- 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.
- Tuple<int, int, int, double> ArithmeticResult;
Now, we can assign a value to it in two ways.
Method 1
- ArithmeticResult = new Tuple<int, int, int, double> (1, 1, 2, 1.11);
Method 2
- 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 .
-
- Tuple<int, int, int, int, int, int, int, Tuple<int, int>> dataWithEightarguments;
-
- Tuple<int, int> innerData = Tuple.Create(2, 3);
-
-
- dataWithEightarguments = new Tuple<int, int, int, int, int, int, int, Tuple<int, int>>(2, 2, 2, 2, 2, 2, 2, innerData);
-
-
- 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.
- public Tuple < int, int, int, double > getArithmeticValues(int a, int b)
- {
- Tuple < int, int, int, double > ArithmeticResult;
- ArithmeticResult = new Tuple < int, int, int, double > (a + b, a - b, a * b, (double) a / b);
- return ArithmeticResult;
- }
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.
- Tuple < int, int, int, double > result = p.getArithmeticValues(a, b);
- Console.WriteLine("Addition: " + result.Item1);
- Console.WriteLine("Subtractions: " + result.Item2);
- Console.WriteLine("Multiplication: " + result.Item3);
- 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.
- public void StoreMoreThanSevenItemsinTuple() {
- Tuple < int, int, int, int, int, int, int, Tuple < int, int >> dataWithEightarguments;
- Tuple < int, int > innerData = Tuple.Create(2, 3);
- dataWithEightarguments = new Tuple < int, int, int, int, int, int, int, Tuple < int, int >> (2, 2, 2, 2, 2, 2, 2, innerData);
- var innerDataReading = dataWithEightarguments.Rest;
- }