Hello, messing around with C# and C++ I have found that the following type of expressions is valid:
int A, B, C, D, E;
A = B = C = D = E = 1;
Based on this action, I'm assigning 1 to E
, E
to , D
and so on, until I get to the end.
My main question is the following, why is this type of expression valid?
According to the IL ( Generated by DotNetFiddle ) when performing this action I get this:
.method public hidebysig static void Main() cil managed
{
//
.maxstack 2
.locals init (int32 V_0,
int32 V_1,
int32 V_2,
int32 V_3,
int32 V_4)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: dup
IL_0003: stloc.s V_4
IL_0005: dup
IL_0006: stloc.3
IL_0007: dup
IL_0008: stloc.2
IL_0009: dup
IL_000a: stloc.1
IL_000b: stloc.0
IL_000c: ret
} // end of method Program::Main
And the IL generated by the following assignment:
A = 0; B = 0; C = 0; D = 0; E = 0;
It is the following:
.method public hidebysig static void Main() cil managed
{
//
.maxstack 1
.locals init (int32 V_0,
int32 V_1,
int32 V_2,
int32 V_3,
int32 V_4)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: stloc.1
IL_0005: ldc.i4.0
IL_0006: stloc.2
IL_0007: ldc.i4.0
IL_0008: stloc.3
IL_0009: ldc.i4.0
IL_000a: stloc.s V_4
IL_000c: ret
}
I don't understand much about IL but according to the code above, the method that assigns one variable to another takes up more space in memory and so on, or something similar (Please correct me if I'm wrong) .
My question is, do both codes really do the same thing?
Below is the explanation of the IL of the first case
In this case, the algorithm that is followed is to place the value
1
on the stack and then duplicate that value to store it temporarily in order to continue assigning it after assigning it to the corresponding variable, first aV_5
then aV_4
and so on until no more temporary storage is needed. the variable, in the end the stack is left empty and the value1
assigned to all the variablesIn the second case assuming that the expression was:
A = 1; B = 1; C = 1; D = 1; E = 1;
In this case, the algorithm simply consists of stacking the value 1 and assigning it to the corresponding variable. This is repeated 5 times.
As we can see in this case in the stack there is only one element at a time, since it is always stacked and unstacked immediately, in the previous case there were up to two
1
since the same value was duplicated each time instead of stacking one same value 5 times.Now answering why the first syntax in C# is valid is due to the following
The
= Operator
has a return value and it is the same value that is assigned to the variable on the left side, that is:equals
and executing it step by step:
The second statement contains multiple assignment expressions, an assignment expression assigns a value to a variable:
But that value can be an assignment expression itself:
The assignment expression
B = 1
that results in is first evaluated1
and that value is assigned toA
. The same process is repeated for all variables in the assignment chain.This is found in the language specification 7.13.1
At the IL level the code is different, however the result is the same when assigning the same value to all the variables.