Tuples and anonymous
Tuples
- Tuples are useful when we do not want to create a full-fledged class but rather special objects that will group other objects together.
- A tuple is a type of structural representation of data. It may be a record in a table.
- By default the .NET Framework supports tuples ranging from one element to 8 elements.
- It is also possible to create a tuple with n number of elements.
System.Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>
Where TRest is a generic.
For example: var numbers = new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,Tuple<Int32>> (1, 2, 3, 4, 5, 6, 7, new Tuple<Int32>(19));
var num_1to3 = Tuple.Create(1, 2, 3);
Items in the preceding tuple are retrieved as num_1to3.Item1, num_1to3.Item2, num_1to3.Item3
var num_3to10 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(4, 5, 6, 7, 8, 9, 10, num_1to3);
Items in the preceding tuple for the first 7 elements are retrieved as num_3to10.Item1, num_1to3.Item2, num_1to3.Item3 and so on.
Elements of num_1to3 that is nested with num_3to10 are retrieved as Rest.Item1, Rest.Item2 and Rest.Item3.
var num_11to16 = new Tuple< int, int, int, int, int, int,
Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>>
(11, 12, 13, 14, 15, 16, num_3to10);
A third-level nested tuple is retrieved as Rest.Rest.Item1, Rest.Rest.Item2 and Rest.Rest.Item3.
From the preceding example we are combining multiple tuple to form a new tuple with n number of elements.
By this way we can eliminate the limitation of elements.
Tupel.Ceate is a static method. It supports only up to 8 elements. To initiate a tuple with more than 8 elements we are forced to use the constructor of the Tuple class.
It is very useful to multiple values as a single parameter.
The following are the methods supported by tuple:
| Name | Description of Tuples |
| Create<T1>(T1) | Creates a new 1-tuple, or singleton. |
| Create |
Creates a new 2-tuple, or pair. |
| Create |
Creates a new 3-tuple, or triple. |
| Create<T1, T2, T3, T4>(T1, T2, T3, T4) | Creates a new 4-tuple, or quadruple. |
| Create<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5) | Creates a new 5-tuple, or quintuple. |
| Create<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6) | Creates a new 6-tuple, or sextuple. |
| Create<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) | Creates a new 7-tuple, or septuple. |
| Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1, T2, T3, T4, T5, T6, T7, T8) | Creates a new 8-tuple, or octuple. |
Disadvantages
- Elements of a tuple are sequentially named such as Item1, Item2 and so on. Custom naming of elements are not possible.
- If the data is large its seems to be difficult to identify the elements.
- Using anonymous types we can eliminate this problem.
- But anonymous types need to be cast to the original type. Refer to the following example.

Santhakumar MunuswamyPosted Jul 11, 2015, 3:10 AM
Nice article! Thanks for sharing
Gowtham RajamanickamPosted Jul 10, 2015, 5:04 AM
Good one..
Sibeesh VenuPosted Jul 10, 2015, 4:50 AM
God one.
Harieswaran DPosted Jul 10, 2015, 12:29 AM
Thanks Friends Rakesh Kalluri,Nilesh Jadav,Narasimha Reddy Chennupalli
Rakesh KalluriPosted Jul 10, 2015, 12:26 AM
Good Article ..
Nilesh JadavPosted Jul 10, 2015, 12:04 AM
Good article sir :0
Pankaj Kumar ChoudharyPosted Jul 9, 2015, 11:03 PM
Nice Explanation Sir.......
Narasimha Reddy ChennupalliPosted Jul 9, 2015, 10:40 PM
Good one..