[ACCEPTED]-Does a List<T> guarantee that items will be returned in the order they were added?-collections

Accepted answer
Score: 71

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

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

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

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

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

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

You can read more about 1 it on MSDN.

Score: 11

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

Insertion

List<T>.Add Method

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

Item parameter is:

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

List<T>.AddRange Method

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

Collection 8 parameter is:

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

Retrieval

List<T>.Enumerator Structure

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

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

Score: 5

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

Ref: List Class

0

Score: 2

Yes according to this MSDN Forum thread

0

More Related questions