foreach纯粹是“语法糖”吗? [英] Is foreach purely “syntactic sugar”?

查看:144
本文介绍了foreach纯粹是“语法糖”吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆