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

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


当前回答

你可以用三种不同的方法

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);
}

其他回答

您还可以使用OperationResult

public OperationResult DoesSomething(int number1, int number2)
{
// Your Code
var returnValue1 = "return Value 1";
var returnValue2 = "return Value 2";

var operationResult = new OperationResult(returnValue1, returnValue2);
return operationResult;
}

当你的方法是异步的,你想返回多个属性。你必须这样做:

public async Task<(int, int)> GetMultipleValues(){
   return (1,2);
}

要么返回类实例,要么使用out参数。这里有一个out参数的例子:

void mymethod(out int param1, out int param2)
{
    param1 = 10;
    param2 = 20;
}

这样叫它:

int i, j;
mymethod(out i, out j);
// i will be 20 and j will be 10

在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#的未来版本将包括命名元组。 看看channel9的演示 https://channel9.msdn.com/Events/Build/2016/B889

跳到13:00讲元组的内容。这将允许如下内容:

(int sum, int count) Tally(IEnumerable<int> list)
{
// calculate stuff here
return (0,0)
}

int resultsum = Tally(numbers).sum

(视频中不完整的例子)