The ECMA CLI spec has the following statement in the description for the initobj
CLI instruction:
"If typeTok is a value type, then after this instruction is executed, the instance is ready for a constructor method to be called."
However, the following C# code (where S
is a struct):
S s = default;
S s2 = new S();
S s3 = new S(5);
compiles to IL that looks something like this:
IL_0001: ldloca.s s
IL_0003: initobj S
IL_0009: ldloca.s s2
IL_000b: initobj S
IL_0011: ldloca.s s3
IL_0013: ldc.i4.5
IL_0014: call instance void S::.ctor(int32)
My question is, when would a compiler ever use initobj
followed by calling the value type's constructor?