如何在一个语句中分配变量列表

use*_*358 7 c# perl

Perl有能力:

my ($a,$b,$c,$d) = foo(); 
Run Code Online (Sandbox Code Playgroud)

其中foo返回4个变量,而不是在同一时间进行指定一个.C#中有类似的东西吗?

Mar*_*ell 8

不,基本上.选项:

object[] values = foo();
int a = (int)values[0];
string b = (string)values[1];
// etc
Run Code Online (Sandbox Code Playgroud)

要么:

var result = foo();
// then access result.Something, result.SomethingElse etc
Run Code Online (Sandbox Code Playgroud)

要么:

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
Run Code Online (Sandbox Code Playgroud)

  • 如果你列举了错误的选项,我猜你还可以包含一个`Tuple <int,sting,foo>``:).显然,C#将用户指向第二个选项 - 返回一个有意义的类. (3认同)