Różnica między instancją wywołania a instancją newobj w IL
Zagłębiam się w C # w Depth i bawię się z dopuszczalnymi typami wartości. Do celów eksperymentalnych napisałem fragment kodu:
private static void HowNullableWorks()
{
int test = 3;
int? implicitConversion = test;
Nullable<int> test2 = new Nullable<int>(3);
MethodThatTakesNullableInt(null);
MethodThatTakesNullableInt(39);
}
I byłem zaskoczony, widząc toimplicitConversion / test2 zmienne są inicjowane za pomocą:
call instance void valuetype [mscorlib]System.Nullable`1<int32>::.ctor(!0)
instrukcja, natomiast kiedyMethodThatTakesNullableInt nazywa się, widzę:
IL_0017: initobj valuetype [mscorlib]System.Nullable`1<int32>
i
IL_0026: newobj instance void valuetype [mscorlib]System.Nullable`1<int32>::.ctor(!0)
co rozumiem. Myślałem, że zobaczęnewobj instrukcja dlaimplicitConversion / test2 także.
To jest pełny kod IL:
.method private hidebysig static void HowNullableWorks() cil managed
{
// Code size 50 (0x32)
.maxstack 2
.locals init ([0] int32 test,
[1] valuetype [mscorlib]System.Nullable`1<int32> implicitConversion,
[2] valuetype [mscorlib]System.Nullable`1<int32> test2,
[3] valuetype [mscorlib]System.Nullable`1<int32> CSZagłębiam się w C # w Depth i bawię się z dopuszczalnymi typami wartości. Do celów eksperymentalnych napisałem fragment kodu:0000)
IL_0000: nop
IL_0001: ldc.i4.3
IL_0002: stloc.0
IL_0003: ldloca.s implicitConversion
IL_0005: ldloc.0
IL_0006: call instance void valuetype [mscorlib]System.Nullable`1<int32>::.ctor(!0)
IL_000b: nop
IL_000c: ldloca.s test2
IL_000e: ldc.i4.3
IL_000f: call instance void valuetype [mscorlib]System.Nullable`1<int32>::.ctor(!0)
IL_0014: nop
IL_0015: ldloca.s CSZagłębiam się w C # w Depth i bawię się z dopuszczalnymi typami wartości. Do celów eksperymentalnych napisałem fragment kodu:0000
IL_0017: initobj valuetype [mscorlib]System.Nullable`1<int32>
IL_001d: ldloc.3
IL_001e: call void csharp.in.depth._2nd.Program::MethodThatTakesNullableInt(valuetype [mscorlib]System.Nullable`1<int32>)
IL_0023: nop
IL_0024: ldc.i4.s 39
IL_0026: newobj instance void valuetype [mscorlib]System.Nullable`1<int32>::.ctor(!0)
IL_002b: call void csharp.in.depth._2nd.Program::MethodThatTakesNullableInt(valuetype [mscorlib]System.Nullable`1<int32>)
IL_0030: nop
IL_0031: ret
} // end of method Program::HowNullableWorks