foreach纯粹是“语法糖”吗? [英] Is foreach purely “syntactic sugar”?
问题描述
当 foreach时,编译器将
与数组一起使用。然后,编译器将 foreach
循环编译为类似 for
的循环 foreach
循环编译为类似 while
的循环,当 foreach
与 IEnumerable
或 IEnumerable< T>
一起使用。那么这是否意味着 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?
推荐答案
它纯粹是语法糖,没有它,您可以获得相同的行为,是的。其他很多东西都是一样的... 用于
,同时
等...要错误引用阿基米德:给我 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没有
No, the CLR doesn't have any concept of foreach
.
这篇关于foreach纯粹是“语法糖”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!