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(T1, T2) |
Creates a new 2-tuple, or pair. |
Create(T1, T2, T3) |
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.
Example Program
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace Tuples_and_anonymoustypes
- {
- class Program
- {
-
-
-
-
- static void Main(string[] args)
- {
- string fulldetails = "harieswaran Sady 9600914291";
- var temp = split_tuples(fulldetails);
- Console.WriteLine("Using Tuples\n");
- Console.WriteLine("name ={0}\n", temp.Item1);
- Console.WriteLine("Nick name ={0}\n", temp.Item2);
- Console.WriteLine("mobile ={0}\n", temp.Item3);
- var ano_type = cast((split_ano_type(fulldetails)), new {
- name = "", nickname = "", mobile = 10000000000000000
- });
- Console.WriteLine("Using Anonymous Types\n");
- Console.WriteLine("name ={0}\n", ano_type.name);
- Console.WriteLine("Nick name ={0}\n", ano_type.nickname);
- Console.WriteLine("mobile ={0}\n", ano_type.mobile);
- Console.ReadKey();
- }
- private static object split_ano_type(string fulldetails) {
- string[] list = new string[2];
- list = fulldetails.Split(' ');
- return new {
- name = list[0], nickname = list[1], mobile = Convert.ToInt64(list[2])
- };
- }
- private static Tuple < string, string, Int64 > split_tuples(string fulldetails) {
- string[] list = new string[2];
- list = fulldetails.Split(' ');
- return Tuple.Create < string, string, Int64 > (list[0], list[1], Convert.ToInt64(list[2]));
- }
- static T cast < T > (object fulldet, T Type)
- {
- return (T) fulldet;
- }
- }
- }