|
Tuples are mainly used for below purposes: 1. Multiple values can be returned without using out paramert or ByRef. 2. Multiple values can be passed through single parameter. 3. Accessing and manipulation of data using tupple is easy. Tuple can be instantiated using new or Create
Tuple<int, double> tpl1 = new Tuple<int, double>(100, 100.100);
OR
Tuple<int, double> tpl1 = Tuple.Create<int, double>(100, 100.100);
Multiple values can be returned without using out(C#) paramert or ByRef(VB.NET).
This example shows the sum of integer and double using the same method and return both sum of integer and double without using out.
Step 1: Create two touples and pass it to a method called Add.
Tuple<int, double> tpl1 = new Tuple<int, double>(100, 100.100); Tuple<int, double> tpl2 = new Tuple<int, double>(100, 100.100); Tuple<int, double> sum = Add(tpl1, tpl2); Response.Write("Sum of Integer = " + sum.Item1 + "<br/>"); Response.Write("Sum of double = " + sum.Item2 + "<br/>");
Step 2: Add method will add integer and double and return the sum of integer and double using Tuple.
private Tuple<int, double> Add(Tuple<int, double> tpl1, Tuple<int, double> tpl2) { int intSum = tpl1.Item1 + tpl2.Item1; double dblSum = tpl1.Item2 + tpl2.Item2; return new Tuple<int, double>(intSum, dblSum); }
The value of a Tuple can be accessed using Item property.As we have defined int and then double, so Item1 will be integer value and Item2 will be double.
The return type is int for Item1 in below image.
The return type is double for Item2 in below image.

private Tuple<int, double> Add(Tuple<int, double> tpl1, Tuple<int, double> tpl2) { int intSum = tpl1.Item1 + tpl2.Item1; double dblSum = tpl1.Item2 + tpl2.Item2; return new Tuple<int, double>(intSum, dblSum); }
By above example we could return int as well as double without using out parameter. Multiple values can be passed through single parameter
This example shows the ease with which we can pass List of integer and list of double using a single tuple to another method.
Step 1: Create two List, List<int>() and List<double>() will hold integers and double values respectively.
List<int> lstInt = new List<int>(); lstInt.Add(100); lstInt.Add(200); lstInt.Add(300); List<double> lstDbl = new List<double>(); lstDbl.Add(100.100); lstDbl.Add(200.200); lstDbl.Add(300.300);
Step 2: Create a Tuple with List<int> and List<double> which will be passed to Add method.
Tuple<List<int>, List<double>> tplLst = new Tuple<List<int>, List<double>>(lstInt, lstDbl); Tuple<int, double> tplSum = Add(tplLst); Response.Write("Sum of Integer = " + tplSum.Item1 + "<br/>"); Response.Write("Sum of double = " + tplSum.Item2 + "<br/>")
Step 3: Create Add method to sum List of int and double return the sum as Tuple of int and double.
private Tuple<int, double> Add(Tuple<List<int>, List<double>> tplLst) { int sumInt = 0; double sumDbl = 0; foreach (int i in tplLst.Item1) { sumInt = sumInt + i; } foreach (double i in tplLst.Item2) { sumDbl = sumDbl + i; } return new Tuple<int, double>(sumInt, sumDbl); }
The above two examples clearly show the ease with which we can access and manipulate data.
foreach and Tuples
Tuple<int, string>[] tpl1 = { Tuple.Create<int, string>(100, "ABC"), Tuple.Create<int, string>(200, "XYZ"), Tuple.Create<int, string>(300, "XYZ") }; foreach (var tpl in tpl1) { Response.Write(tpl.Item1 + " " + tpl.Item2 + "<br/>"); }
It's easy to iterate through array of tupples.
var tple = Tuple.Create<int, string>(100, "ABC"); foreach (var tp in tple) { }
The above statement will give below error because IEnumerable is not implemented in Tuple.
foreach statement cannot operate on variables of type 'System.Tuple<int,string>' because 'System.Tuple<int,string>' does not contain a public definition for 'GetEnumerator'
Number of items in a Tuple
Tuple<int, string, double, string, int, string, double, string> tpl = Tuple.Create<int, string, double, string, int, string, double, string>(1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123, "XYZ"); The number of Item which can be passed in a Tuple is 7, if 8 Items specifed in the Tuple like below, the errr below error will be generated.
Cannot implicitly convert type 'System.Tuple<int,string,double,string,int,string,double,System.Tuple<string>>' to 'System.Tuple<int,string,double,string,int,string,double,string>'
The above error specifies that 8th item should be generic Tuple object. Even intellisense also gives guidance of using Tuple object as 8th item.
The below statement will work with 8 tuple items.
Tuple <int, string, double, string, int, string, double, Tuple<string>> tpl = Tuple.Create<int, string, double, string, int, string, double, string>(1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123, "My Rest Item");
In the below image the 8th Item is Tuple which contains only one string but no Tuple has been used while assigning the value. <img src="ArticleImages/Tuple TRest and 8th Item.jpg" alt="Tuple TRest and 8th Item"/>
But the same doesn't hold good if Tuple is used with two Tuple items inside the Tuple
Tuple<int, string, double, string, int, string, double, Tuple<int,string>> tpl = Tuple.Create<int, string, double, string, int, string, double, int, string>(1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123,100, "My Rest Item");
The above statement will produce below error.
Using the generic method 'System.Tuple.Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1, T2, T3, T4, T5, T6, T7, T8)' requires 8 type arguments
The correct way of using Tuple with more than 8 item is to use provide 8th Item as another Tuple.
Tuple<int, string, double, string, int, string, double, Tuple<int,string>> tpl =new Tuple<int, string, double, string, int, string, double, Tuple<int, string>>(1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123, new Tuple<int, string>(100, "My Rest Item"));
Item of Tuple inside Tuple can be accessed like
tpl.Rest.Item1

|