给定下面的代码,Bar(int y) 中的方法参数 y 会被赋值为 x 还是 1?我意识到它们在逻辑上是等价的,但我想了解赋值操作。
class Program
{
static void Main(string[] args)
{
var foo = new Foo();
var x = 0;
foo.Bar(x = 1);
}
}
public class Foo
{
public void Bar(int y)
{
}
}
最佳答案
参数获取赋值的值。
考虑这样的代码:
int x = 0;
int y = (x = 1);
x = 42;
foo.Bar(y);
即使 x
被再次更改,y
仍然包含 1
。
关于c# - C#中的方法参数赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3637268/