For 和 Foreach 的比较

来源:互联网 发布:ubuntu 学打字 编辑:程序博客网 时间:2024/05/16 10:21
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

C#代码:public int LoopTest()  {      int s=0;                int[] array={2,3,4,5,6,7,8,9};      For(int k=0;k<=7;k++)   {     s+=array[k];    }                              

   Foreach(int a in array)   {     s+=a;   }                        

    return s;

  }

 

IL代码:.method public hidebysig instance int32  LoopTest() cil managed{  // 代码大小       78 (0x4e)  .maxstack  3  .locals init ([0] int32 s,           [1] int32[] 'array',           [2] int32 k,           [3] int32 a,           [4] int32 CS$00000003$00000000,           [5] int32[] CS$00000007$00000001,           [6] int32 CS$00000008$00000002)  IL_0000:  ldc.i4.0  IL_0001:  stloc.0  IL_0002:  ldc.i4.8  IL_0003:  newarr     [mscorlib]System.Int32  IL_0008:  dup  IL_0009:  ldtoken    field valuetype '<PrivateImplementationDetails>'/'$$struct0x6000002-1' '<PrivateImplementationDetails>'::'$$method0x6000002-1'  IL_000e:  call       void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array,                                                                                                      valuetype [mscorlib]System.RuntimeFieldHandle)  IL_0013:  stloc.1  IL_0014:  ldc.i4.0  IL_0015:  stloc.2  IL_0016:  br.s       IL_0022         //For 开始的地方  IL_0018:  ldloc.0  IL_0019:  ldloc.1  IL_001a:  ldloc.2  IL_001b:  ldelem.i4  IL_001c:  add  IL_001d:  stloc.0  IL_001e:  ldloc.2  IL_001f:  ldc.i4.1  IL_0020:  add  IL_0021:  stloc.2  IL_0022:  ldloc.2  IL_0023:  ldc.i4.7  IL_0024:  ble.s      IL_0018    //For 结束的地方  IL_0026:  ldloc.1     //Foreach 从这里开始  IL_0027:  stloc.s    CS$00000007$00000001  IL_0029:  ldc.i4.0  IL_002a:  stloc.s    CS$00000008$00000002  IL_002c:  br.s       IL_003e  IL_002e:  ldloc.s    CS$00000007$00000001  IL_0030:  ldloc.s    CS$00000008$00000002  IL_0032:  ldelem.i4  IL_0033:  stloc.3  IL_0034:  ldloc.0  IL_0035:  ldloc.3  IL_0036:  add  IL_0037:  stloc.0  IL_0038:  ldloc.s    CS$00000008$00000002  IL_003a:  ldc.i4.1  IL_003b:  add  IL_003c:  stloc.s    CS$00000008$00000002  IL_003e:  ldloc.s    CS$00000008$00000002  IL_0040:  ldloc.s    CS$00000007$00000001  IL_0042:  ldlen  IL_0043:  conv.i4  IL_0044:  blt.s      IL_002e //Foreach结束的地方  IL_0046:  ldloc.0  IL_0047:  stloc.s    CS$00000003$00000000 //此乃自动生成的一个变量,保存返回值,void时没有  IL_0049:  br.s       IL_004b  IL_004b:  ldloc.s    CS$00000003$00000000  IL_004d:  ret} // end of method Class1::LoopTest

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击