The answer to the title is pretty obvious but I came across something while going through Microsoft docs which made me post this question.
The result of an assignment expression is the value assigned to the left-hand operand
a = (b = c)
As stated in Microsoft Docs, the result of b=c is the value assigned to the left hand operand ,which is nothing but c. Then why is it not mentioned so? Am I missing anything?
I can’t think of a scenario in which b and c would have different values.If so ,then the assignment operator loses it’s purpose right? But it can be of different types if an implicit cast is possible.
Like the documentation states:
The type of the right-hand operand must be the same as the type of the left-hand operand or implicitly convertible to it.
Notice the last part: convertible.
Let’s say c is of type Something, b is Other (which has implicit conversion from Something) so in that case the value assigned to a is not the same as c, but the same as b.
This is one reason why it’s important to specify that the return value is what was assigned to b. It may not be c.
Also if we consider clause such as b = 1 + Method() * 7
then it’s obviously better to say that the return value is whatever was assigned to b instead of explaining it’s the same as 1 + Method() * 7
. The letters in the explanation don’t stand for variables, they can be whatever is allowed on the right side.