tags:

views:

96

answers:

3

Hi all!

Can one rely that enumerating on the items of a List is done by the order of their insertion?

Does anyone know what the spec says about that?

Thanks!

+7  A: 

This will depend on the implementation. Here's a nice overview of the different generic collections. As far as the List<T> implementation is concerned, enumerating will be done in the same order as elements have been added.

Darin Dimitrov
+7  A: 

For a List<T>, yes, you can depend on the ordering.

It is the nature of a list structure, where elements are ordered by the index. Enumeration always takes place by index order.

codekaizen
+1  A: 

by the order of their insertion

That's awkward language in your question. List<> indeed supports inserts anywhere inside the list with the Insert() method. No, enumeration produces the list order, not the insert order. If the question would have said "by the order of their additions" (Add method) then the answer is Yes.

Hans Passant