Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a List<T> guarantee that items will be returned in the order they were added?

Does a List<T> always guarantee that items will be returned in the order they were added when enumerated?

Updated: Thanks for all the answers folks, puts my mind at ease. I had a quick poke around the List<T> class with .NET Reflector (should've probably done that in the first place) and indeed the underlying store is an array of T (T[]).

like image 530
Kev Avatar asked Jan 17 '09 08:01

Kev


People also ask

Is list order guaranteed?

The List<> class does guarantee ordering - things will be retained in the list in the order you add them, including duplicates, unless you explicitly sort the list.

Does Python list guarantee order?

Yes, the order of elements in a python list is persistent.

Does order matter in a list?

(Recall that when using list notation for a set, the order that the members are listed doesn't matter.) Think of a combination as a bunch of items that are thrown into a basket.

Does list preserve order?

Yes. A List, by definition, always preserves the order of the elements. This is true not only of ArrayList, but LinkedList, Vector, and any other class that implements the java.


2 Answers

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.

like image 55
Sander Versluys Avatar answered Sep 20 '22 23:09

Sander Versluys


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.

like image 30
Alex Angas Avatar answered Sep 20 '22 23:09

Alex Angas