当 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纯粹是“语法糖”吗?
发布评论