foreach 纯粹是“语法糖"吗? [英] Is foreach purely “syntactic sugar”?
问题描述
当 foreach
与数组一起使用时,编译器会将 foreach
循环编译为类似于 for
循环的内容.当 foreach
与 IEnumerable
或IEnumerable
.那么这是否意味着 foreach
纯粹是 语法糖
?或者有什么复杂的吗?
The compiler compiles a foreach
loop into something like a for
loop when the foreach
is used with an array. And the compiler compiles a foreach
loop into something like a while
loop when the foreach
is used with an IEnumerable
or IEnumerable<T>
. So does this mean foreach
is purely syntactic sugar
? Or is there anything sophisticated about it?
CLR 知道 foreach
吗?MSIL 代码中是否有专门为 foreach
设计的内容?
Does the CLR know about foreach
? Is there anything specifically designed for foreach
in the MSIL code?
推荐答案
这纯粹是语法糖,因为没有它您可以获得相同的行为,是的.许多其他事情是相同的... for
, while
等等... 错误引用阿基米德:给我 if
和 goto
,我会移动代码..."
It's purely syntactic sugar in that you could obtain the same behaviour without it, yes. Many other things are the same... for
, while
etc... To misquote Archimedes: "Give me if
and goto
, and I will move the code..."
不,CLR 没有任何 foreach
的概念.
No, the CLR doesn't have any concept of foreach
.
这篇关于foreach 纯粹是“语法糖"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!