Ordering of List<T> and other IEnumerables
Can one rely that enumerating on the items of a List is done by the order of their insertion?
Does anyone know wh开发者_开发百科at the spec says about that?
Thanks!
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.
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.
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.
精彩评论