To simulate such a loop foreach, I suggest using a loop for, iterating backwards:
for (int i = ls.Count - 1; i >= 0; --i) {
var v = ls[i];
}
Or if you need to go ahead
int n = ls.Count;
for (int i = 0; i < n; ++i) {
var v = ls[i];
}
If you want to assign added elements (see comments below), a standard loop is enough for:
for (int i = 0; i < ls.count; ++i) {
var v = ls[i];
}
Finally, you can iterate a copy of the source list:
foreach (var v in ls.ToList()) {
...
}
source
share