Can I have List (T) .ForEach (), which moves items in order?

There List(T).ForEach(), which performs an action for each item in the list. However, the MSDN description says nothing about the order in which the elements will go through List, and I need them to go exactly in order.

How can I achieve this with ForEach()?

+5
source share
3 answers

List<T>.ForEachwill go in the same order as a regular loop foreach, which is also the natural order of the list.

Yes, the documentation does not state this, but this is obvious behavior, and I think it’s pretty reasonable to rely on what doesn’t change.

( Parallel.ForEach - , .)

+13

, Queue<T> (LIFO) Stack<T> (FIFO), . LINQ .

+2

A class List<T>represents an ordered set of elements, so repeating it using enumerations or a ForEachmethod will save the order, even if the documentation is not very clear.

+1
source

All Articles