In what order does a C# for each loop iterate over a List?

Basically it’s up to the IEnumerator implementation – but for a List<T> it will always go in the natural order of the list, i.e. the same order as the indexer: list[0], list[1], list[2] etc.

I don’t believe it’s explicitly documented – at least, I haven’t found such documentation – but I think you can treat it as guaranteed. Any change to that ordering would pointlessly break all kinds of code. In fact, I’d be surprised to see any implementation of IList<T> which disobeyed this. Admittedly it would be nice to see it specifically documented…

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)