Ever tried to remove or change something in a C# Enumeration while you iterate over it? Yes, it does not work very well.
But there are ways around it.
foreach (Item item in items.ToArray()) { if (item.Visible == false) { items.Remove(item); } }
The simplest way is to use ToArray(). No much coding, no complicated loops, but obviously not very efficient for larger lists.
A few alternatives are presented by Kevin Ransom.
3 Kommentare zu “Collection was modified; enumeration operation may not execute”
Simple, but effective! Exactly what I needed.
Ha, did a double loop last time. Silly me 🙂
Yep, good idea indeed.