除了确保不能更改它们(以编译器错误为准)之外,JIT是否对const局部变量进行了任何优化?
例如。
public static int Main(string[] args)
{
const int timesToLoop = 50;
for (int i=0; i<timesToLoop; i++)
{
// ...
}
}
最佳答案
生成的IL不同(使用Release模式):
using constant local using normal local
---------------------------------------------------------------------
.entrypoint .entrypoint
.maxstack 2 .maxstack 2
.locals init ( .locals init (
[0] int32 i) [0] int32 timesToLoop,
L_0000: ldc.i4.0 [1] int32 i)
L_0001: stloc.0 L_0000: ldc.i4.s 50
L_0002: br.s L_0008 L_0002: stloc.0
L_0004: ldloc.0 L_0003: ldc.i4.0
L_0005: ldc.i4.1 L_0004: stloc.1
L_0006: add L_0005: br.s L_000b
L_0007: stloc.0 L_0007: ldloc.1
L_0008: ldloc.0 L_0008: ldc.i4.1
L_0009: ldc.i4.s 50 L_0009: add
L_000b: blt.s L_0004 L_000a: stloc.1
L_000d: ret L_000b: ldloc.1
L_000c: ldloc.0
L_000d: blt.s L_0007
L_000f: ret
如您所见,编译器将所有变量用法替换为常量值,从而使堆栈更小。