2011-10-06 4 views
6

私のように定義されたメソッドMultiplyした場合:C#コンパイラがILで追加のOpCodeを発行するのはなぜですか?

public static class Experiment 
{ 
    public static int Multiply(int a, int b) 
    { 
     return a * b; 
    } 
} 

をコンパイラは、このILを放射しない理由その後:

.method public hidebysig static int32 Multiply(int32 a, int32 b) cil managed 
{ 
    .maxstack 2    //why is it not 16? 
    .locals init (
     [0] int32 CS$1$0000) //what is this? 
    L_0000: nop    //why this? 
    L_0001: ldarg.0 
    L_0002: ldarg.1 
    L_0003: mul 
    L_0004: stloc.0   //why this? 
    L_0005: br.s L_0007  //why this? 
    L_0007: ldloc.0   //why this? 
    L_0008: ret 
} 

あなたが見ることができるように、それもないいくつかの追加のオペコードを含んでいます実際に私が次のILを期待しているとき、私には意味をなさないでしょう:

.method public hidebysig static int32 MyMethod(int32 a, int32 b) cil managed 
{ 
    .maxstack 16 
    L_0000: ldarg.0 
    L_0001: ldarg.1 
    L_0002: mul 
    L_0003: ret 
} 

これはまったく同じことです。

なぜ、コンパイラはILで追加のOpCodeを発行するのですか?

私はデバッグモードを使用しています。

+1

設定をデバッグまたはリリースしますか? –

+0

@AlexFarber:Debug。 – Nawaz

+0

少なくとも 'NOP'はデバッグのみであり、JITterが命令を並べ替えることのできない何らかの種類のシーケンスポイントを作成します。これはデバッグに役立ちます。 – CodesInChaos

答えて

関連する問題