简单的好奇,这段代码
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。
就像我说的,这纯粹是好奇。