tags:

views:

136

answers:

3

One of the Tuple Types in .net 4 is a Single-Element Tuple. I just wonder what the purpose of this struct is?

The only use I saw is when using in the 8+ Tuple as it can be assigned to TRest, where it actually makes sense. Is that it? Or is there any other purpose?

+2  A: 

You'd have to ask the BCL designers to be certain, but I suspect that since there is a 1-tuples in the real world, the .NET framework authors wanted to provide equivalent symmetry in their implementation.

Tuples are the .NET implementation of what you'd consider the mathematical concept of a tuple.

Now since you're asking for programming uses for Tuple<T>, I would also answer that there are .NET languages (like F#) that could use Tuple<> for representation of things like return values from functions. Since an F# function could certainly return a 1-tuple as a result - it adds symmetry and consistency to the behavior and feel of the language.

Your example with 8+ tuples is also probably legitimate, in that the Rest property could be a 1-tuple to represent the "overflow".

LBushkin
I don't think it's actually possible to construct a 1-tuple in idiomatic F#, because the comma operator is what creates a tuple in that language. `let foo = 3,5` is the definition of a 2-tuple containing 3 and 5, but the only way to create a 1-tuple in F#, I think, is the same way that you'd do it in C#: `new System.Tuple<int>(5)`
Joel Mueller
Or `Tuple.Create` of course. Still, as far as I know 1-tuples are the only sort that F# does not provide any syntactic sugar for.
Joel Mueller
+1  A: 

All tuple types implement ITuple, so I guess that when you want to have a return type that is an ITuple, you need the option of having a single value, as you suggest.

Oded
The one problem with that is that ITuple is internal to mscorlib, so I can't use it anyway :)
Michael Stum
That explains why MSDN doesn't have any documentation on it, apart from the declaration of the different Tuple types... ;)
Oded
+5  A: 

Tuples automatically implement IStructuralComparable and IStructuralEquatable, among other things. This allows tuples to be compared and sorted right out of the box. From Bill McCarthy's article in December 2009 edition of Visual Studio Magazine, "Types and Tuples in .NET 4":

Although tuples may look simple and nondescript, they do provide strong typing and important comparison and equality functionality. Tuples are useful across method, class or even machine boundaries.

By putting your data type into a tuple, even of only one element, you are guaranteed immutability, equatability, and comparability. For tuples consisting of only one element, the main benefit of using a tuple is going to be immutability: once the tuple is created, it's data can never change for the life of the tuple.

Ben McCormack
I fail to see how immutability, equatability and comparability is achived. If you put mutable data into a tuple you are still able to mutate that data. Similar for equatability and comparability... How would putting the data into a tuple add any (meaningful) kind of comparability?
Johan Kullbom
@johan You bring up a good point, especially as it applies to complex objects. That might make for a good separate question by itself.
Ben McCormack