您的位置:首页 >> 编程开发 >> .NET >> 其它 >> 正文
其它 RSS
 

For 和 Foreach 的比较

http://www.rdxx.com 04年07月24日 12:12 Blog 我要投稿

关键词: 比较

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

共2页  第1页 第2页


 
 
标签: 比较 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站