foreach与for的性能比较

本文介绍了一个使用C#编写的简单示例,展示了如何遍历一个字符串数组并将每个元素追加到文本框中。同时提供了相应的IL(中间语言)代码,帮助读者理解.NET框架下代码的底层实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
1 string [] strings = new string [] { "str1" , "str2" , "str3" , "str4" , "str5" , "str6" };
2              foreach ( string s in strings)
3              {
4                  textBox1.AppendText(s + " " );
5              }

IL:

01              foreach ( string s in strings)
02 000000e0  nop 
03 000000e1  mov         eax,dword ptr [ebp-44h] 
04 000000e4  mov         dword ptr [ebp-50h],eax 
05 000000e7  xor         edx,edx 
06 000000e9  mov         dword ptr [ebp-54h],edx 
07 000000ec  nop 
08 000000ed  jmp         00000136                       //jmp to J1
09 000000ef  mov         eax,dword ptr [ebp-54h] 
10 000000f2  mov         edx,dword ptr [ebp-50h] 
11 000000f5  cmp         eax,dword ptr [edx+4] 
12 000000f8  jb          000000FF          //低于"1个字节全是1"跳转 
13 000000fa  call        65C86690          //地址如此之大,应该是个外部call
14 000000ff  mov         eax,dword ptr [edx+eax*4+0Ch] 
15 00000103  mov         dword ptr [ebp-48h],eax 
16              {
17 00000106  nop 
18                  textBox1.AppendText(s + " " ); </COD < dd>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值