« EnterpriseLibraryContainer and IServiceLocator in Enterprise Library 5 | Main | dynamic and ExpandoObject in ASP.NET MVC with Strongly-Typed ViewPage »

12/30/2009

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Edward J. Stembler

I hate to sound like a broken record, but similar to Zip, Tuple is something Pyrhon has had for ages.

I love that C# is becoming a hybrid language by supporting traditionally dynamic language features. On the other hand, I sometimes cannot help myself from thinking "Why not just use a dynamic language instead?"

Johnnyo

this is great. thanks for sharing this post. the one problem i had with anonymous types is that they couldn't cross method boundaries, but it looks like tuples will solve this problem.

Jon Skeet

Quick bit of pedantry: unless I've missed something (in which case I *really* want to know about it ASAP!) this isn't a feature of C# 4 at all - it's a feature of the .NET 4.0 framework library. The C# *language* doesn't know anything about tuples, does it?

I only ask because the language *could* have known about them - it could have done interesting things with methods returning tuples, for instance, as other languages do.

David Hayden

You're correct, Jon. It is a feature of the .NET Framework 4.

I just loosely labeled it as more of a C# 4 feature because I was using C# in the examples.

Thanks for pointing that out.

Jon Skeet

@David: Phew - I thought I might have an extra section to add into my 2nd edition :)

@Edward: What do Zip and Tuple have to do with dynamic languages? What's "dynamic" about the Zip operator? C# 4 and .NET 4.0 certainly *do* have dynamic features (via the DLR) but tuples and the zip operator are pretty statically typed as far as I can see... in particular, neither require you to lose any static type information.

If you like statically typed languages but you happen to want a tuple or a zip operator, why on earth would you change to a dynamic language just to get them?

Murrayondotnet

I was about to dismiss the entire Tuple feature in favor of Anonymous Types until I read the very last sentence. What allows Tuple to get past the method return boundary, but not Anon. Types?

Thanks.

Marc Gravell

@Murrayondotnet - simply that there is no way of expressing the return type for an anon-type, except for cheating with "object" (and either using "dynamic", reflection, or cast-by-example to get the values back out). Tuples are a well-defined set of types, so can be declared as a return type.

Richard Poole

I really hope this does become a feature of C# 4. I won't want to write "Tuple.Create" any more than I want to write "Enumerable.Select".

Maybe C# still has a lesson or two to learn from Python.

Miguel de Icaza

We have modified Mono's C# compiler to support Tuples natively so that you get Perl-like syntax on assignment and we are extending it to support tuple creation like the sample for F#.

Patch and samples here:

http://tirania.org/blog/archive/2009/Dec-23.html

Paul_spencer

a couple of questions:
When you have a Tuple longer than 8 items, can you just add another Tuple, i.e. can you have Tuples of Tuples?

An advantage of the use of anonymous types is that you can name the properties, i.e. var blah = new { Name = string.Empty };. is there anyway to name the types of a Tuple differently from .Item1 .. .Item8?

David Hayden

Paul,

You have to use nested tuples when you have 8 or more items. The Tuple.Rest Property allows you to access those eight through nth items in the nested tuples.

You can read more here:

http://www.davidhayden.me/2010/01/tuplerest-property-and-nested-tuples.html

As far as I know there is no way to rename the Item1, Item2, etc. properties on a Tuple to make them more meaningful.

The comments to this entry are closed.