在C#中通过函数返回多个变量

时间:2015-09-24 11:56:12

标签: c# asp.net asp.net-mvc

今天我面临一个问题,那就是可能的方法 在C#

中返回函数中的多个值

1是返回对象 但是有什么其他可能的方法呢?

此致

2 个答案:

答案 0 :(得分:2)

您可以使用输出参数:

public void Foo(out int bar, out string goo)
{
    bar = 1;
    goo = "moo";
}

并像这样使用它:

int a;
string b;
Foo(out a, out b);

答案 1 :(得分:1)

您可以使用Tuple返回多个值。请记住,它们可以很快失控。

public Tuple<string, int> Foo()
{
     return Tuple.Create("Bar",1);
}

Tupples可以用多个值旋转。

Tuple<int,string,int,int,string>  

他们可以快速凌乱:)

祝你好运