// Fields
.field private object '<>2_current'
.field private int32 '<>1_state '
.field public class DataStructureLab.People '<>4_this'
.field public int32 '<i>5_1'
// Methods
.method private final hidebysig newslot virtual
instance bool MoveNext () cil managed
{
class Program { static void Main(string[] args) { int i = 999; int j = 888; Console.WriteLine( i + j); } }該段程式碼對應的未被優化的 IL 程式碼(存在很多 nop 指令,它是空指令):
.class private auto ansi beforefieldinit AssemblyLab.Program extends [mscorlib] System.Object { // Methods .method private hidebysig static void Main (string[] args) cil managed { //Method begins at RVA 0x207c // Code size 15 ( Oxf) .maxstack 2 .entrypoint .locals init ( [0] int32 i, [1] int32 j ) IL_0000: nop IL_0001: ldc.i4 999 IL_0006: stloc.0 IL_0007: ldc.i4 888 IL_000c: stloc.1 IL_000d: ldloc.0 IL_000e: ldloc.1 IL_000f: add IL_0010: call void [mscorlib]System.Console::WriteLine(int32) IL_0015: nop IL_0016: ret } // end of method Program::Main .method publie hidebysig specialname rtspecialname instance void .ctor () cil managed { // Method begins at RVA 0x2 097 // Code size 7 ( 0x7 ) maxstack 8 L_0000: ldarg.0 L_0001: call instance void [mscorlib]System.Obj ect::.ctor() IL_0006: ret } // end of method Program::.ctor } // end of class AssemblyLab. ProgramMain 方法的大部分程式碼都含有一個助記符。其中,nop 是編譯器在 Debug 模式下插入的方便我們偵錯設定斷點的空操作,所以,這裡我們就忽略 nop。
.method private hidebysig static void Main (string[] args) cil managed
IL 指令 .method 指出後面的程式碼為一個方法。IL 特性 private 指出該方法是私有的 (如果一個方法在 C# 中,沒有顯式給出可見性關鍵字,則預設的關鍵字是 private)。.maxstack 2 .entrypoint .locals init ( [0] int32 i, [1] int32 j )由於程式碼僅僅有兩個變數,因此棧的最大空間為2。之後,.entrypoint 指令指示編譯器, 程式碼的入口點在此。
.method public hidebysig specialname rtspecialname instance void .ctor () cil managed { //Method begins at RVA 0x2097 // Code size 7 ( 0x7) .maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL_0006: ret } // end of method Program:: . ctorProgram類Main方法的IL程式碼主體如下:
IL_0000: nop IL_0001: ldc.i4 999 IL_0006: stloc.0 IL_0007: ldc.i4 888 IL_000c: stloc.1 IL_000d: ldloc.0 IL_000e: ldloc.1 IL_000f: add IL_0010: call void [mscotlib]System.Console::WriteLine(int32) IL_0015: nop IL_0016: ret0001 行載入了第一個變數(通過 ldc.i4), 其中,i4 代表 int32 型別,而後面的 999 則是變數的值。