+-
C#中的方法参数赋值
给定下面的代码,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#中的方法参数赋值 - 乐贴网