问题描述
在通过阵列使用的foreach迭代,有没有担保,在其中的元素返回的顺序是订单阵[0],数组[1],数组[2],...
When iterating through an array using foreach, are there any guaranties that the order in which the elements are returned is the order array[0], array[1], array[2], ...
我知道这是怎么了Array类现在已经实现,但没有任何担保的框架的未来版本?同样的问题也适用于清单<>
I know this is how the Array class is implemented now but are there any guaranties for future versions of the framework? The same questions goes for List<>.
推荐答案
我不得不同意与所有的答案为止。
I'd have to disagree with all the answers so far.
首先,C#3.0的标准保证的foreach的顺序阵列上:
First, the C# 3.0 standard guarantees the order of foreach on an array:
在其中的foreach遍历顺序 数组的元素,是作为 如下:对于一维数组 元素遍历增加 索引顺序,从索引0 索引长度结束 - 1。 多维数组,元件是 的遍历,使得指数 最右边的维度增加 第一,那么下一个左维, 等的左边。
- C#语言规范3.0版,第240页。
-- C# Language Specification Version 3.0, page 240.
二,在对象上,的foreach(C#)和使用的MoveNext每个(VB.NET)工作,复位,并在对象上目前的成员(的)。这些都是典型IEnumerator接口的一部分。
Second, on objects, foreach (C#) and For Each (VB.NET) work by using the MoveNext, Reset, and Current members on an object (source). These are typically part of the IEnumerator interface.
在有一个订单(读:即实现IList或IList的(T)的东西)的集合,这意味着该元素将在该命令所返回的后备存储存储它们
In collections that have an order (read: things that implement IList or IList(T)), this means that the elements will be returned in the order that the backing store stores them.
这篇关于排序顺序阵列上使用的foreach时,列举等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!