我希望能够在 C# 中不使用临时变量的情况下交换两个变量。这能做到吗?
decimal startAngle = Convert.ToDecimal(159.9);
decimal stopAngle = Convert.ToDecimal(355.87);
// Swap each:
// startAngle becomes: 355.87
// stopAngle becomes: 159.9
最佳答案
C# 7 introduced tuples这使得无需临时变量即可交换两个变量:
int a = 10;
int b = 2;
(a, b) = (b, a);
这会将 b
分配给 a
并将 a
分配给 b
。
关于c# - 在不使用临时变量的情况下交换两个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/804706/