请问在C#中的foreach循环保证评价的订单? [英] Does the foreach loop in C# guarantee an order of evaluation?

查看:154
本文介绍了请问在C#中的foreach循环保证评价的订单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从逻辑上讲,人们会认为,在C#中的foreach循环将在相同的顺序循环的递增评估。在实验上,它的作用。然而,似乎是在MSDN网站没有这样的确认。

Logically, one would think that the foreach loop in C# would evaluate in the same order as an incrementing for loop. Experimentally, it does. However, there appears to be no such confirmation on the MSDN site.

难道仅仅是这样一个明显的答案,他们没有想到,包括在网站上的信息?或者是有,它会运行不稳定的可能性?

Is it simply such an apparent answer that they did not think to include that information on the site? Or is there the possibility that it will behave erratically?

推荐答案

有关阵列(注意,的System.Array 工具的IEnumerable ),它会按顺序访问的元素。对于其他类型( 的IEnumerable ,或有的GetEnumerator ),它访问的顺序元素提供,通过交替的 的MoveNext 和的 当前 来电。

For arrays (note that System.Array implements IEnumerable), it will access elements in order. For other types (IEnumerable, or having GetEnumerator), it accesses elements in the order provided, through alternating MoveNext and Current calls.

标准规定(ECMA-364§15.8.4):

The standard states (ECMA-364 §15.8.4):

,其中的foreach遍历
数组的元素的顺序,是
如下:对于一维数组
元素都在增加
索引顺序,从指数0和
索引Length结束走过 - 1.对于
多维阵列,元件是
遍历使得
最右边的维的索引先增加
,则下一个左维,
和等向左侧。

"The order in which foreach traverses the elements of an array, is as follows: For single-dimensional arrays elements are traversed in increasing index order, starting with index 0 and ending with index Length – 1. For multi-dimensional arrays, elements are traversed such that the indices of the rightmost dimension are increased first, then the next left dimension, and so on to the left."

这篇关于请问在C#中的foreach循环保证评价的订单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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