foreach纯粹是“语法糖”吗?

编程入门 行业动态 更新时间:2024-10-11 01:20:58
本文介绍了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纯粹是“语法糖”吗?

本文发布于:2023-11-25 14:56:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   foreach

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!