Good, fiddling with C # and C ++ I've found that the following type of expressions is valid:
int A, B, C, D, E;
A = B = C = D = E = 1;
According to this action, I am assigning 1 in E
, E
in D
and so on, until the end.
My question principal is this, why is this kind of expression valid?
According to the IL ( Generated by DotNetFiddle ) when doing 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 do not understand much about IL but according to the previous code, the method that assigns a variable in another, occupies more space in memory and so, or something similar (Please correct me if I'm wrong) .
My question is, do both codes really do the same thing?