C
C#17mo ago
exove

❔ Need help explaining precedence in C#

int x = 10;
int y = 20;
x += y += y = 5;
Console.WriteLine(x) //Output 35
int x = 10;
int y = 20;
x += y += y = 5;
Console.WriteLine(x) //Output 35
Why is the above code does it evaluate to 35? Why is it not y = 5 y += y // 10 x += y // 20
13 Replies
circles.png
circles.png17mo ago
so first x += y (x is 30, y is 20) then 30 is used from that expression x += y = 5 (y is set to 5 then added to x)
exove
exove17mo ago
but isnt += and = right associative? so why would x += y be first "Right-associative operators are evaluated in order from right to left. The assignment operators, the null-coalescing operators, lambdas, and the conditional operator ?: are right-associative. For example, x = y = z is evaluated as x = (y = z)."
reflectronic
reflectronic17mo ago
right, y += y = 5 is first remember that y += y = 5 becomes y = y + (y = 5) the y on the left of the + is read before the assignment so y = 20 + (y = 5) then 25 is assigned to y, and becomes the value of the expression
exove
exove17mo ago
not sure i understant y = y + (y = 5) so y = 5 is first so y = 5 + 5
reflectronic
reflectronic17mo ago
no, y = 5 is not first
exove
exove17mo ago
why if its right associative
reflectronic
reflectronic17mo ago
+ is left associative well, + has higher precedence than =
exove
exove17mo ago
but so += is right, but + is left
reflectronic
reflectronic17mo ago
well, i don't think it matters in this case. y = y + (y = 5) can only be intrpreted one way. y = a + b means "evaluate a, then evaluate b, then add them." in this case, a is y and b is (y = 5) to put it another way, the associativity only describes how to add parentheses to an expression. so, if you have a + b + c, if you were to add parentheses that kept the expression's value the same, it would be ((a + b) + c), because + is left associative for a = b = c, it would be (a = (b = c)), because = is right associative you cannot add any more parentheses to y = y + (y = 5), so the associativity doesn't do anything (well, you could do y = (y + (y = 5)), but that happens because of operator precedence, not associativity)
exove
exove17mo ago
i think i understand more, ty
canton7
canton717mo ago
Operand evaluation order is a separate concept to precendence, see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#1141-general In particular, look at the example. When evaluating an expression, the operands are evaluated first, left to right. Then the operations between those operands are performed. Precedence determines the order that those operations are performed in, but it does not affect the order in which the operands are evaluated. So, back to x += y += y = 5;. We first expand the += (see §11.19.3):
x = x + (y = y + (y = 5));
x = x + (y = y + (y = 5));
Then we evaluate the operands:
x = 10 + (y = 20 + (y = 5))
x = 10 + (y = 20 + (y = 5))
Then we evaluate the operations between the operands, taking precedence (and the parens) into account:
x = 10 + (y = 20 + 5)
x = 10 + 25
x = 35
x = 10 + (y = 20 + 5)
x = 10 + 25
x = 35
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts