Software Architecture – C# – 4.0 – New Feature – Tuple

Tuple is a generic type that provide an option to group together different type of object in one piece.
According to MSDN:

System.Tuple is a fixed-size collection of heterogeneously typed data. Like an array, a tuple has a fixed size that can’t be changed once it has been created. Unlike an array, each element in a tuple may be a different type, and a tuple is able to guarantee strong typing for each element.

1. Tuple can be created by using constructor as

//Method for 1 for creating tuple
Tuple<int, string> myTuple = new Tuple<int, string>(1, "Shahzad");
Tuple<int, string, double, string> myTuple2 = new Tuple<int, string, double, string>(1,"Shahzad",33,"Lahore");
//Printing the items
Console.WriteLine("myTuple.Item1:" + myTuple.Item1); // Output : 1
Console.WriteLine("myTuple.Item2:" + myTuple.Item2); // Output : Shahzad

2. Tuple can also be created by using static method create of System.Tuple as:

// Method for 2 for Creating Tuple
var myTuple3 = Tuple.Create(1, "Shahzad");
var myTuple4 = Tuple.Create(1, "Shahzad", 33, "Lahore");
//Printing the items
Console.WriteLine("myTuple.Item1:" + myTuple3.Item1);// Output : 1
Console.WriteLine("myTuple.Item2:" + myTuple3.Item2);// Output : Shahzad

Nested Tuple:
Nested Tuple allow us to create a hierarchy of item by creating a tuple as item in other tuples as:

 // Nested Tuples
 Tuple<double, double, double, double> myRestTuple2 = new Tuple<double, double, double, double>(2.1, 2.4, 2.5, 2.7);
Tuple<double, double, double, double> myRestTuple1 = new Tuple<double, double, double, double>(3.1, 3.4, 3.5, 3.7);
Tuple<int, Tuple<double, double, double, double>, Tuple<double, double, double, double>> myTuple5 = new Tuple<int, Tuple<double, double, double, double>, Tuple<double, double, double, double>>(1, myRestTuple1, myRestTuple2);

// Printing the items
Console.WriteLine("Print First Item:" + myTuple5.Item1); // Output : 1
Console.WriteLine("Print First Item of second item(tuple):" + myTuple5.Item2.Item1); // Output : 3.1
Console.WriteLine("Print Second Item of second item(tuple):" + myTuple5.Item2.Item2);// Output : 3.4
Console.WriteLine("Print Third Item of second item(tuple):" + myTuple5.Item2.Item3);
// Output : 3.5
Console.WriteLine("Print fourth Item of second item(tuple):" + myTuple5.Item2.Item4);// Output : 3.7

There are only 8 items allowed in a tuple by default .If you to add more items, nested tuple can be used as:

// Nested Tuples for having items more then 8
Tuple<int, int, int, int> myRestTuple = new Tuple<int, int, int, int>(8, 9, 10, 11);
Tuple<int, int, int, int, int, int, int, Tuple<int, int, int, int>> myTuple6 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int, int>>(1, 2, 3, 4, 5, 6, 7, myRestTuple);
 Console.WriteLine("First item : "+ myTuple6.Item1);  // Outpt : 1
 Console.WriteLine("Second item : " + myTuple6.Item2);// Outpt : 2
 Console.WriteLine("Third item : " + myTuple6.Item3);// Outpt : 3
 Console.WriteLine("Forth item : " + myTuple6.Item4);// Outpt : 4
 Console.WriteLine("Fifth item : " + myTuple6.Item5);// Outpt : 5
 Console.WriteLine("Sixth item : " + myTuple6.Item6);// Outpt : 6
 Console.WriteLine("Seventh item : " + myTuple6.Item7);// Outpt : 7
 Console.WriteLine("Eightth item : " + myTuple6.Rest.Item1);// Outpt : 8
 Console.WriteLine("Nineth item : " + myTuple6.Rest.Item2);// Outpt : 9
 Console.WriteLine("Tenth item : " + myTuple6.Rest.Item3);// Outpt : 10
 Console.WriteLine("Eleventh item : " + myTuple6.Rest.Item4);// Outpt : 11

Tuples vs Anonymous Type
Anonymous type can’t be passed as return type from a method, however Tuples can be passed as input parameters and return type.
Items of Tuples can’t be identified with name, they are just called item1,item2,item3…itemn. However items of anonymous type can be named.
With introduction of Tuples, C# has provided one more feature of functional programming languages.
Enjoy Coding…

2 Responses to Software Architecture – C# – 4.0 – New Feature – Tuple

  1. Peter Gfader says:

    Hi Shahzad

    I think we should avoid the Tuple type, because it decreases Readability of code.

    What do u think?


  2. shahzadsb says:

    hi peter,

    Your refactoring code with class works, but it not logically an entity as class in OOPs terms (not having method, inheritance, abstraction)
    So I think Microsoft is trying to avoid confusion/mixing concept with OOPs philosophy. So they introduced just grouping of related types with tuple.

    Secondly, your point regarding readability of code with tuples is very much right. Microsoft should have introduced some method of named properties in tuple.

    Have a nice day.
    Shahzad Sarwar

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: