Assigning a method parameter in C #

Given the code below, will the method parameter in Bar (int y) have a value of x or 1? I understand that they are logically equivalent, but I want to understand the assignment operation.

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)
    {
    }
}
+3
source share
5 answers

The parameter receives the assignment value.

Consider the following code:

int x = 0;
int y = (x = 1);
x = 42;
foo.Bar(y);

While xchanging again, ystill contains 1.

+6
source

It is assigned to the result x=1, which is 1.

+4
source

Bar, " , " ( ).

int 1.

+3

. . .

x = 1 . (x=1), , x, bar.

x = 1
foo.bar(x)

, , x foo.

+1

, () y, , int.

, , x - , , 1, x 1, y = x = 1.

0

Source: https://habr.com/ru/post/1763112/


All Articles