排序顺序阵列上使用的foreach时,列举等 [英] Sort order when using foreach on an array, list etc

查看:336
本文介绍了排序顺序阵列上使用的foreach时,列举等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过阵列使用的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。   多维数组,元件是   的遍历,使得指数   最右边的维度增加   第一,那么下一个左维,   等的左边。

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.

- <一个href="http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/CSharp%20Language%20Specification.doc">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时,列举等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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