2.25.2010

The Tuple Type in the Microsoft .NET Framework 4.0

My first impression of the new System.Tuple type in the .NET 4.0 Framework was not a good one. Probably because at first look it seems like an implementation of the already existing System.Collections.Generic KeyValuePair. As you would expect, Microsoft had a few compelling reasons for this change.

Many of the MS language teams each created their own private version of the tuple. After some time, the teams within MS learned of this and wanted to use it in their code as well. After collaboration between the Base Class Libraries (BCL) and the Managed Extensibility Framwork (MEF) teams, they decided to include it in the upcoming Framework release.

So we know that MS uses tuples in their code. I can not even remember the last time I used KeyValuePair outside the use of a Dictionary collection. As I am writing this, I can think of a few ways I could have used KeyValuePair more in my code.

With the upcoming release of F# language, interoperability was paramount. Having different representations of a tuple in C# and F# would have led to coding woes. As it says in Microsoft's documentation, "any time you wanted to call a method from an F# assembly that took a tuple as an argument, you would be unable to use the normal C# syntax for a tuple or pass an existing C# tuple. Instead, you would be forced to convert your C# tuple to an F# tuple, and then call the method." Tuples are a core concept in functional languages, so it doesn't hurt to be exposed to it.

Some more interesting things about Tuples:
There is a maximum of 8 elements in a tuple. The eighth tuple must be of type tuple, making it limitless.
Tuples are reference types.