This question already has an answer here:
I'm trying to remove an item from an ArrayList
and I get this Exception:
Collection was modified; enumeration operation may not execute.
Any ideas?
This question already has an answer here:
I'm trying to remove an item from an ArrayList
and I get this Exception:
Collection was modified; enumeration operation may not execute.
Any ideas?
I agree with several of the points I've read in this post and I've incorporated them into my solution to solve the exact same issue as the original posting.
That said, the comments I appreciated are:
"unless you are using .NET 1.0 or 1.1, use
List<T>
instead ofArrayList
. ""Also, add the item(s) to be deleted to a new list. Then go through and delete those items." .. in my case I just created a new List and the populated it with the valid data values.
e.g.
Am I missing something? Somebody correct me if I'm wrong.
Here's an example (sorry for any typos)
OR if you're using 3.5, Linq makes the first bit easier:
Replace "..." with your condition that determines if item should be removed.