我读了这个问题的c++版本,但并没有真正理解它。

有人能解释清楚,如果它可以在c#中完成,以及如何?


当前回答

在c# 7中有一个新的元组语法:

static (string foo, int bar) GetTuple()
{
    return ("hello", 5);
}

你可以返回一个记录:

var result = GetTuple();
var foo = result.foo
// foo == "hello"

你也可以使用新的解构器语法:

(string foo) = GetTuple();
// foo == "hello"

但是要小心序列化,所有这些都是语法糖——在实际编译的代码中,这将是一个Tuple<string, int>(根据接受的答案),包含Item1和Item2,而不是foo和bar。这意味着序列化(或反序列化)将使用这些属性名。

因此,对于序列化,声明一个记录类并返回它。

c# 7中的另一个新特性是改进了out形参的语法。你现在可以内联声明out,这在某些情况下更适合:

if(int.TryParse("123", out int result)) {
    // Do something with result
}

但是,大多数情况下,您将在. net自己的库中使用它,而不是在您自己的函数中。

其他回答

不,在c#中(对于低于c# 7的版本),你不能从一个函数返回多个值,至少不能像在Python中那样。

然而,也有一些选择:

您可以返回一个object类型的数组,其中包含您想要的多个值。

private object[] DoSomething()
{
    return new [] { 'value1', 'value2', 3 };
}

你可以使用out参数。

private string DoSomething(out string outparam1, out int outparam2)
{
    outparam1 = 'value2';
    outparam2 = 3;
    return 'value1';
}

你可以试试这个

public IEnumerable<string> Get()
 {
     return new string[] { "value1", "value2" };
 }

在c# 7及以上版本中,可以看到这个答案。

在以前的版本中,你可以使用。net 4.0+的元组:

例如:

public Tuple<int, int> GetMultipleValue()
{
     return Tuple.Create(1,2);
}

有两个值的元组有Item1和Item2作为属性。

有几种方法可以做到这一点。你可以使用ref参数:

int Foo(ref Bar bar) { }

这将向函数传递一个引用,从而允许函数在调用代码的堆栈中修改对象。虽然这在技术上不是一个“返回”值,但它是一种让函数做类似事情的方法。在上面的代码中,该函数将返回一个int和(可能)修改bar。

另一种类似的方法是使用out形参。out形参与ref形参相同,只是附加了编译器强制的规则。这条规则是,如果你将一个输出参数传递给一个函数,该函数需要在返回之前设置它的值。除此之外,out形参的工作原理与ref形参类似。

最后一种方法(在大多数情况下是最好的)是创建一个封装两个值的类型,并允许函数返回该类型:

class FooBar 
{
    public int i { get; set; }
    public Bar b { get; set; }
}

FooBar Foo(Bar bar) { }

最后一种方法更简单,更容易阅读和理解。

你可以用三种不同的方法

1. Ref / out参数

使用参考:

static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    int add = 0;
    int multiply = 0;
    Add_Multiply(a, b, ref add, ref multiply);
    Console.WriteLine(add);
    Console.WriteLine(multiply);
}

private static void Add_Multiply(int a, int b, ref int add, ref int multiply)
{
    add = a + b;
    multiply = a * b;
}

使用:

static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    int add;
    int multiply;
    Add_Multiply(a, b, out add, out multiply);
    Console.WriteLine(add);
    Console.WriteLine(multiply);
}

private static void Add_Multiply(int a, int b, out int add, out int multiply)
{
    add = a + b;
    multiply = a * b;
}

2. 结构/类

使用结构体:

struct Result
{
    public int add;
    public int multiply;
}
static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    var result = Add_Multiply(a, b);
    Console.WriteLine(result.add);
    Console.WriteLine(result.multiply);
}

private static Result Add_Multiply(int a, int b)
{
    var result = new Result
    {
        add = a * b,
        multiply = a + b
    };
    return result;
}

使用类:

class Result
{
    public int add;
    public int multiply;
}
static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    var result = Add_Multiply(a, b);
    Console.WriteLine(result.add);
    Console.WriteLine(result.multiply);
}

private static Result Add_Multiply(int a, int b)
{
    var result = new Result
    {
        add = a * b,
        multiply = a + b
    };
    return result;
}

3.元组

Tuple类

static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    var result = Add_Multiply(a, b);
    Console.WriteLine(result.Item1);
    Console.WriteLine(result.Item2);
}

private static Tuple<int, int> Add_Multiply(int a, int b)
{
    var tuple = new Tuple<int, int>(a + b, a * b);
    return tuple;
}

c# 7元组

static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    (int a_plus_b, int a_mult_b) = Add_Multiply(a, b);
    Console.WriteLine(a_plus_b);
    Console.WriteLine(a_mult_b);
}

private static (int a_plus_b, int a_mult_b) Add_Multiply(int a, int b)
{
    return(a + b, a * b);
}