C# 同層級大括號內不可重複宣告同名變數;IL 則將其視為不同區域變數(如 V_0、V_1),不受區塊作用域限制。
// C# 代碼
class zzz {
static bool i = true;
public static void Main() {
while (i) {
System.Console.WriteLine("hi");
}
}
}
IL 關鍵點:
i 在 .cctor 初始化為 1(true)。br.s IL_000c 跳至條件檢查處。IL_000c:ldsfld 載入 i,brtrue.s IL_0002 若為真則進入循環體 IL_0002。.method public hidebysig static void vijay() il managed {
.entrypoint
br.s IL_000c
IL_0002: ldstr "hi"
call void [mscorlib]System.Console::WriteLine(string)
IL_000c: ldsfld bool zzz::i
brtrue.s IL_0002
ret
}
brtrue:堆疊頂為非零(true)則跳轉。brfalse:堆疊頂為零(false)則跳轉。這展示了 IL 以條件分支實現高階循環,且布爾值在 IL 中實為整數 0/1。