I am studying a simple C # IL example and cannot understand anything. I have a very simple program:
void Main()
{
C c = new C(1);
}
class C
{
public C(){}
public C(int i){}
}
have CIL:
IL_0001: ldc.i4.1
IL_0002: newobj UserQuery+C..ctor
IL_0007: stloc.0 // c
C..ctor:
IL_0000: ldarg.0
IL_0001: call System.Object..ctor
IL_0006: nop
IL_0007: nop
IL_0008: nop
IL_0009: ret
C..ctor:
IL_0000: ldarg.0
IL_0001: call System.Object..ctor
IL_0006: nop
IL_0007: nop
IL_0008: nop
IL_0009: ret
I do not understand how a virtual machine will distinguish what a call to one constructor should be. There are two identical labels, and the only difference seems to be that the main argument calls the argument. Is there anything deeper when calling the constructor? Maybe the compiler provides some metadata to distinguish which one should be named?
So let's say the following:
void Main()
{
C c = new C(1);
}
class C
{
public C(){}
public C(int i){ i += 1;}
}
IL_0001: ldc.i4.1
IL_0002: newobj UserQuery+C..ctor
IL_0007: stloc.0 // c
C..ctor:
IL_0000: ldarg.0
IL_0001: call System.Object..ctor
IL_0006: nop
IL_0007: nop
IL_0008: nop
IL_0009: ret
C..ctor:
IL_0000: ldarg.0
IL_0001: call System.Object..ctor
IL_0006: nop
IL_0007: nop
IL_0008: ldarg.1
IL_0009: ldc.i4.1
IL_000A: add
IL_000B: starg.s 01
IL_000D: nop
IL_000E: ret
Now, how to distinguish the called, at the label level, we can not distinguish it.
source
share