C# 4.0/BCL 4 Series: System.Tuple
I mentioned, in my last post, that I was using C# 4.0 In A Nutshell to better my C# 3 and to come up to speed with C# 4/BCL 4. Another cool thing in Framework 4.0 is the generic set of classes for holding a set of differently typed objects called tuples. Like the Func<> and Action<> delegates, tuples also take up to 8 parameters:
public class Tuple<T1>
up to:
public class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>
While C# and VB.NET do not have the concept as part of the core language, many functional languages have tuples as common features. Also many teams have built their own. The BCL 4 System.Tuple is the one definition to rule all others and there is one interoperable type.
Above, each of the tuples has read-only properties called Item1, Item2 and so on (one for each type parameter). You can instantiate a tuple either via it's constructor:
var t = new Tuple<int, string> (123, "Hello");
or via the static helper method Tuple.Create:
Tuple<int, string> t = Tuple.Create(123, "Hello");
which can be made:
var t = Tuple.Create(123, "Hello");
You can then access the properties as (Note: each is statically typed):
Console.WriteLine(t.Item1 * 2); // 246
Console.WriteLine(t.Item2.ToUpper()); // HELLO
You may ask what Tuples are good for? They are really convenient in returning more than one value from a method or creating collections of value pairs. You could use object arrays but then you lose static type safety, incur the cost of boxing/unboxing for value types, and require clumsy casts that cannot be validated by the compiler.
Now to return to the subject of the last post, comparisons. Tuples are classes so they are reference types. This means, comparing two distinct instances with the equality operator returns false. Unlike arrays however, the Equals method is overridden to compare each individual element instead:
var t1 = Tuple.Create(123, "Hello");
var t2 = Tuple.Create(123, "Hello");
Console.WriteLine(t1 == t2); // False
Console.WriteLine(t1.Equals (t2)); // True
Last time, I talked about the StructuralComparison type. Well, tuples implement IStructuralEquatable, so you can pass in a custom equality comparer.
If you want to learn a lot more about System.Tuple, you can look at the excellent MSDN article: "CLR Inside Out: Building Tuple" http://msdn.microsoft.com/en-us/magazine/dd942829.aspx

Pingback from Twitter Trackbacks for Framework 4.0: System.Tuple : Sam Gentile's Blog (if (DeveloperTask == Communication && OS == Windows) [samgentile.com] on Topsy.com
Pingback from Friday Links #100 | Blue Onion Software *
Might as well make this a series. I mentioned in the first post , "I have been spending quite a bit of time re-focusing and reviewing C# (and some BCL) fundamentals, with an eye to what's different in C# 4.0/BCL 4.0. The reason for this, is though
I mentioned, in my last post, that I was using C# 4.0 In A Nutshell to better my C# 3 and to come up to speed with C# 4/BCL 4. Another cool thing in Framework 4.0 is the generic set of classes for holding a set of differently typed objects called tuples