tags:

views:

380

answers:

9

I'm looking at the new C# feature of tuples. I'm curious, what problem was the tuple designed to solve?

What have you used tuples for in your apps?

Update

Thanks for the answers thus far, let me see if I have things straight in my mind.

A good example of a tuple has been pointed out as coordinates. Does this look right?

var coords = Tuple.Create(geoLat,geoLong);

Then use the tuple like so:

var myLatlng = new google.maps.LatLng(" + coords.Item1 + ", " + coords.Item2 + ");

Is that correct?

+5  A: 

It's often helpful to have a "pair" type, just used in quick situations (like returning two values from a method). Tuples are a central part of functional languages like F#, and C# picked them up along the way.

Stephen Cleary
Thanks, I updated the question with more specifics...
Chad
+1  A: 

very useful for returning two values from a function

Keith Nicholas
rather like a struct then?
KevinDTimm
Maybe for wrapping an anonymous type?
bloparod
A: 

I find the KeyValuePair refreshing in C# to iterate over the key value pairs in a Dictionary.

jskaggz
`KeyValuePair` can be viewed as special-purpose workaround. In Python, iterating over a `dict` just returns (key, value) tuples.
dan04
Yes, like python. :-) I wasn't aware that KeyValuePair in c# wasn't a tuple?
jskaggz
+1  A: 

A Tuple is often used to return multiple values from functions when you don’t want to create a specific type. If you're familiar with Python, Python has had this for a long time.

Randy Minder
+1  A: 

A common use might be to avoid creating classes/structs that only contains 2 fields, instead you create a Tuple (or a KeyValuePair for now). Usefull as a return value, avoid passing N out params...

+4  A: 

It provides an alternative to ref or out if you have a method that needs to return multiple new objects as part of its response.

It also allows you to use a built-in type as a return type if all you need to do is mash-up two or three existing types, and you don't want to have to add a class/struct just for this combination. (Ever wish a function could return an anonymous type? This is a partial answer to that situation.)

Toby
Thank you, that's a good explanation.
Chad
+1  A: 

Returning more than one value from a function. getCoordinates() isn't very useful if it just returns x or y or z, but making a full class and object to hold three ints also seems pretty heavyweight.

Dean J
+11  A: 

When writing programs it is extremely common to want to logically group together a set of values which do not have sufficient commonality to justify making a class.

Many programming languages allow you to logically group together a set of otherwise unrelated values without creating a type in only one way:

void M(int foo, string bar, double blah)

Logically this is exactly the same as a method M that takes one argument which is a 3-tuple of int, string, double. But I hope you would not actually make:

class MArguments
{
   public int Foo { get; private set; } 
   ... etc

unless MArguments had some other meaning in the business logic.

The concept of "group together a bunch of otherwise unrelated data in some structure that is more lightweight than a class" is useful in many, many places, not just for formal parameter lists of methods. It's useful when a method has two things to return, or when you want to key a dictionary off of two data rather than one, and so on.

Languages like F# which support tuple types natively provide a great deal of flexibility to their users; they are an extremely useful set of data types. The BCL team decided to work with the F# team to standardize on one tuple type for the framework so that every language could benefit from them.

However, there is at this point no language support for tuples in C#. Tuples are just another data type like any other framework class; there's nothing special about them. We are considering adding better support for tuples in hypothetical future versions of C#. If anyone has any thoughts on what sort of features involving tuples you'd like to see, I'd be happy to pass them along to the design team. Realistic scenarios are more convincing than theoretical musings.

Eric Lippert
Excellent info. I still can't think of a good scenario to use tuples in my apps, but understand the concept better now. Thank you!
Chad
Very interesting to see the beginning of language convergence between C# and F#. Might things like `Asynch.Parallel` turn up in hypothetical future versions of C#?
MalcomTucker
@MalcomTucker: Asychrony and parallelism are definitely on our minds as rich areas where language tools could be used to solve real problems. I don't think that F# style asynchronous workflows are necessarily the best fit for C#, but they definitely are inspiring.
Eric Lippert
While tuples are useful, one big drawback is clarity. It's hard to read and understand code that refers to `Item1`, `Item2`, etc... If tuples ever do achieve language support in C#, it would be wonderful to allow their members to be named (or at least aliased) in manner that allows code that uses them to be more understandable. In such a hypthetical future, I'd also love to see tuples of appropriate "shape" as legal params to methods that take individual parameters (and vice versa). So, `Tuple<int,string,bool>` can be passed to `M(int,string,bool)`. It would make memoizing methods much easier.
LBushkin
+2  A: 

Tuples provide an immutable implementation of a collection

Aside from the common uses of tuples:

  • to group common values together without having to create a class
  • to return multiple values from a function/method
  • etc...

Immutable collections are inherently thread safe:

Immutable objects can be useful in multi-threaded applications. Multiple threads can act on data represented by immutable objects without concern of the data being changed by other threads. Immutable objects are therefore considered to be more thread-safe than mutable objects.

From "Immutable Object" on wikipedia

Evan Plaice
Thank you for adding additional info to the question. Much appreciated.
Chad