7

Perl has the ability to do:

my ($a,$b,$c,$d) = foo(); 

where foo returns 4 variables, as opposed to assigning it one at a time. Is there something similar in C#?

RobEarl
  • 7,862
  • 6
  • 35
  • 50
user2521358
  • 279
  • 1
  • 3
  • 11

3 Answers3

8

No, basically. Options:

object[] values = foo();
int a = (int)values[0];
string b = (string)values[1];
// etc

or:

var result = foo();
// then access result.Something, result.SomethingElse etc

or:

int a;
string b;
float c; // using different types to show worst case
var d = foo(out a, out b, out c); // THIS WILL CONFUSE PEOPLE and is not a 
                                  // recommendation
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • @lazyberezovsky ultimately all of these things are ways of expressing multiple values outbound from a function, and assigning them to locals at the caller; the *mechanism* for that (lists, etc) is the implementation details – Marc Gravell Jul 29 '13 at 13:10
  • 3
    If you're enumerating bad options, I guess you can also include a `Tuple` `:)`. Obviously, C# points the users to the second option - return a meaningful class. – Kobi Jul 29 '13 at 13:13
  • Eric Lippert talks about tuples with a bit of inside knowledge here: http://stackoverflow.com/questions/3089706/what-requirement-was-the-tuple-designed-to-solve. When I used a tuple to solve a similar problem in my current position, the guy I was working with said he wished he'd seen it before. Apparently, the team have a long history of creating new classes just to hold values when returning data from methods. – Rob Lyndon Jul 31 '13 at 08:27
2

Tuple can be a useful construct for this.

public Tuple<int, string, double> Foo() { ... }

Then you can do:

var result = Foo();

int a = result.Item1;
string b = result.Item2;
double c = result.Item3;

This is a legacy of the increasing influence of functional programming styles on C#: the tuple is a fundamental construct in many functional languages, and greatly assists in their static typing.

Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
1

For functions you must return either 1 object or void. But you can approach this problem several ways.

  1. You can create a data structure such as a struct or a class that will contain a,b,c,d and return that as your function e.g. data foo() data will contain a,b,c,d
  2. You can use the out keyword in the parameter of your function e.g. foo(out a, out b, out c, out d), but your variable inputs will need to be initialized. More info here. See http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.80).aspx
  3. You can also use ref which is similiar to out. See http://msdn.microsoft.com/en-US/library/14akc2c7(v=vs.80).aspx
  4. Or if a,b,c,d are all the same type you can return them in the form of a collection as an arrray or a list as another member has pointed out

Also remember depending on the type that you are passing strcut vs objects that you value may be passed as a Value or Reference. See http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx

rbtLong
  • 1,542
  • 3
  • 14
  • 31