List < T > 是否保证项目按添加顺序返回?

List<T>是否总是保证项目将按照枚举时添加的顺序返回?

更新 : 感谢所有的回答,朋友们,让我的头脑放松。我快速浏览了一下 List<T>课程。NET 反射器(可能一开始就应该这么做) ,实际上底层存储是一个 T(T[])数组。

22065 次浏览

Yes according to this MSDN Forum thread

Yes. But it's not part of the specification.

Ref: List Class

The List is index based and new items will always be added to the end of the list. You can insert items at a certain index so the next items will move one position.

So yes, you can use it safely that way...

The List(T) class is the generic equivalent of the ArrayList class. It implements the IList(T) generic interface using an array whose size is dynamically increased as required.

Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

The List(T) is not guaranteed to be sorted. You must sort the List(T) before performing operations (such as BinarySearch) that require the List(T) to be sorted.

A List(T) can support multiple readers concurrently, as long as the collection is not modified. Enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with one or more write accesses, the only way to ensure thread safety is to lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

You can read more about it on MSDN.

Yes, List<T> guarantees both insertion order and retrieval order and this is documented on MSDN (emphasis below is mine).

Insertion

List<T>.Add Method

Adds an object to the end of the List<T>.

Item parameter is:

The object to be added to the end of the List<T>.

List<T>.AddRange Method

Adds the elements of the specified collection to the end of the List<T>.

Collection parameter is:

The collection whose elements should be added to the end of the List<T>.

Retrieval

List<T>.Enumerator Structure

Initially, the enumerator is positioned before the first element in the collection. At this position, Current is undefined. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current.

Current returns the same object until MoveNext is called. MoveNext sets Current to the next element.

All of the answers under this questions are wrong. List does not guarantee the retrieval order; therefore, the question about the insertion order is moot. In real life tests, the retrieval order may or may not be the same as insertion order. However, Microsoft has collections that guarantee retrieval orders, but it does not expose them to developers. Those collections are all of the *ItemCollections used in Winforms GUI controls.

I struggled with the issue or insertion/retrieval order of List and had a problem whereby even Insert(list.Count, item) resulted in the new items being at zero position at retrieval, needless to say Add or Append, and the only solution was to ditch databinding and to Add items directly into the control's Items. That fixed the issue. Microsoft apparently enjoys torturing and misleading us, in its perpetual quest for bad karma.