単純な好奇心、このコード
private const long constLong = 16;
private static long instanceLong = 16;
static long constTest()
{
long i = 4;
return i + constLong;
}
static long instanceTest()
{
long i = 4;
return i + instanceLong;
}
この IL を生成します。
.field private static literal int64 constLong = int64(16)
.field private static int64 instanceLong
.method private hidebysig static int64 constTest () cil managed
{
// Method begins at RVA 0x2068
// Code size 9 (0x9)
.maxstack 2
.locals init (
[0] int64 i
)
IL_0000: ldc.i4.4
IL_0001: conv.i8
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: ldc.i4.s 16
IL_0006: conv.i8
IL_0007: add
IL_0008: ret
} // end of method Program::constTest
.method private hidebysig static int64 instanceTest () cil managed
{
// Method begins at RVA 0x2080
// Code size 11 (0xb)
.maxstack 2
.locals init (
[0] int64 i
)
IL_0000: ldc.i4.4
IL_0001: conv.i8
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: ldsfld int64 ConsoleApplication1.Program::instanceLong
IL_0009: add
IL_000a: ret
} // end of method Program::instanceTest
constTest() にldc.i4.sがあるのはなぜですか?
IL_0004: ldc.i4.s 16
IL_0006: conv.i8
ldc.i8の代わりに:
IL_0004: ldc.i8 16
constTest() は conv.i8 を実行する必要があるためです。
私が言ったように、これは純粋な好奇心です。