使用方法的 return 同时分配给多个变量

Assign to multiple variable at the same time using method's return

所以,我不确定这样的事情是否可行。可能是?只是好奇...

// Initial values of the strings.
string variable1 = "Something", variable2 = "SomethingElse";

// New values for the strings. ** LEFT OF ASSIGNMENT OPERATOR WON'T COMPILE **
(variable1, variable2) = DoSomething(variable1, variable2);

// Method to modify the initial strings.
public List<string> DoSomething(string v1, string v2)
{ ...; return new List<string> { v1, v2 }; }

您可能问的是语法上类似于

的问题

C#7 Tupples

它的优点 属性,除了比通常的声明更短 Tuple<T> 之外,您可以拥有命名元素,从代码可读性和维护的角度来看,这是一件大事。

如果您仅询问实施的语义C#今天[=28]一次返回 2 double 个值=],你显然可以使用 Tuple<T>,比如

Tuple<double, double> = DoSomething();