Tuple types in .Net Framework 4.0

Tuple types introduced in .Net Framework 4.0

Tuple types are  present in functional languages like Haskell and also in dynamic languages like Python. F# also has its language specific implementation of  Tuple types from version 1.0.

With the release of  .Net Framework 4.0, Tuples have been added to Base Class Library which will surely be better than language specific implementation.

Tuples provide a way of grouping elements of different data types. Tuples are the data values that can contain N items. For object-oriented programming, tuples provide a quick way to link several values together. This can be useful when you wish to return multiple, related values from a method or property.  The .NET Framework directly supports tuples with one to seven elements. In addition, you can create Tuples of eight or more elements by nesting tuple objects in the Rest property of a  Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object.

public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable

public class Tuple<T1, T2> : I…

public class Tuple<T1, T2, T3, T4, T5, T6, T7> : I…

public class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : I…

.Net Framework also provides System.Tuple class. This  is the factory class that provides static methods to create different Tuple objects of the above mentioned Tuple Types:

public static class Tuple

{

public static Tuple<T1> Create<T1>(T1 item1);

public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2);

public static Tuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3);

public static Tuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 item1, T2 item2, T3 item3, T4 item4);

public static Tuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5);

public static Tuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6);

public static Tuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7);

public static Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>> Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, T8 item8);

}

Example:

Using Direct Generic Tuple Types:

var t2 = new Tuple<int,int,int,int,int,int,int,Tuple<int,int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int, int>(8, 9));

Using Factory Class:

var t1 = Tuple.Create(1, 2, 3, 4, 5, 6, 7, new Tuple<int, int>(8, 9));

Accessing output:

t1.prop1, t1.prop2, t1.prop3, t1.prop4…t1.prop#

With Tuple types the properties are assigned their item# names by Microsoft and one cannot change this. And this is also true that these names doesn’t have any semantics (real meaning) behind them. That is why it is the responsibility of Producer and Consumer to assign meaning to them. It means that producer and consumer must have clear understanding of what is being returned in Item# properties. Surely, it reduces code readability. So there should be proper comments to avoid confusion.

Tuple types are usually compared with Anonymous types. The basic advantage that Tuples have over anonymous types is that tuples can be used as return values or method parameters.

Tuple types override ToString() method of Object class in an interesting way. If we call ToString() on tuple objects it will return a comma separated string of all of its values.

Other helpful links –

http://stackoverflow.com/questions/3017352/is-using-tuples-in-my-net-4-0-code-a-poor-design-decision

http://msdn.microsoft.com/en-us/library/system.tuple.aspx

One thought on “Tuple types in .Net Framework 4.0

Leave a Reply

Your email address will not be published. Required fields are marked *


one + 4 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>