在C#中,是纯粹的foreach一个“语法糖”?或者是有什么关于它的更深? [英] In c#, is foreach purely a “syntactic sugar”? Or is there anything deeper about it?

查看:287
本文介绍了在C#中,是纯粹的foreach一个“语法糖”?或者是有什么关于它的更深?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器编译了foreach循环变得像一个for循环当的foreach是用于数组;

The compiler compiles a "foreach" loop into something like a for loop when the "foreach" is used with an array;

和编译器编译了foreach循环成类似当年的foreach是使用一个IEnumerable或IEnumerable的while循环;

And the compiler compiles a "foreach" loop into something like a while loop when the "foreach" is used with an IEnumerable or IEnumerable;

那么,这是否意味着的foreach是一个纯粹的语法糖?

So does this mean "foreach" is purely a "syntactic sugar"?

或者有什么复杂的呢?

请问CLR知道的foreach?有没有具体的MSIL code专为的foreach什么?

Does the CLR know about "foreach"? Is there anything specifically designed for "foreach" in the MSIL code?

在此先感谢。

推荐答案

这是纯粹的语法糖中,你可以得到相同的行为,如果没有它,是的。许多其他的东西都是一样的... ,而组合等,错误引用阿基米德:给我如果 GOTO ,我将撬动了code ...

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.

这篇关于在C#中,是纯粹的foreach一个“语法糖”?或者是有什么关于它的更深?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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