Return Multiple Values from Methods with Tuples -- Visual Studio Magaz

added by pwhe23
12/12/2011 10:05:40 AM

7 Kicks, 404 Views

You have a method that returns multiple values. So you have to define a class, instantiate it, set the properties to the values, and return the resulting object, right? Well, no you don't -- at least in .NET Framework 4, which gives you a simpler way: Tuples.


12/12/2011 10:06:09 AM
I think tuples are *ok* for certain situations, but I feel like if they're being used in a public or heavily used API, it's just inviting disaster should the requirements change. Not as easy to refactor code for a change to the tuple.
Maybe I'm just being paranoid ;-)

12/19/2011 6:05:56 PM
I agree with this actually... they're more suited to pure functional languages where they find more natural usage context -- or if in C#, then at least in a functional style of C# code.

12/12/2011 10:08:06 AM
Yeah, I'm not sure this is actually a good idea. Just because you *can* doesn't mean you *should*.

12/20/2011 8:37:03 AM
Tuples in .NET - I never thought I would see the day. Although in this case why not just define some output parameters? Or is that too old school?

12/20/2011 8:53:24 AM
At least the output params would be part of the function signature and provide some error-checking and refactoring support.

12/20/2011 1:40:38 PM
With the examples shown there are better alternatives than returning tuples. That said, tuples have their place, particularly if the class shown will be used from F#. There's the added advantage (or disadvantage depending upon your viewpoint) that tuples are immutable.

1/6/2012 11:09:22 AM
To me this feels like it hides some of the intent. I think that naming variables, parameters, functions and the like go a long way to making things clearer, whereas this seems to obscure the true intent or return value which then needs further deciphering, especially if people start doing this in an API. I do like the functional nature of it, but I don't think I'll be able to convince myself this will work in most places, maybe very few specific scenarios, but more than that, probably not...