Order List <T> and Other IEnumerables

Is it possible to assume that the enumeration of list elements is performed in the order in which they are inserted?

Does anyone know what spec says about this?

Thanks!

+4
source share
3 answers

It will be implementation dependent. Here is a good overview of the various general collections. As for the implementation of List<T> , the enumeration will be performed in the same order in which the elements will be added.

+7
source

With a List<T> , yes, you can depend on the order.

This is the nature of the list structure, where items are ordered by index. Enumeration is always performed in index order.

+7
source

in the order of their insertion

This awkward language is in your question. List <> really supports inserts anywhere inside the list using the Insert () method. No, an enumeration creates a list order, not an insertion order. If the question would say "in the order they were added" (the "Add" method), then the answer will be "Yes."

+1
source

Source: https://habr.com/ru/post/1314656/


All Articles