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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are removing the item during a
foreach, yes? Simply, you can’t. There are a few common options here:List<T>andRemoveAllwith a predicateiterate backwards by index, removing matching items
use
foreach, and put matching items into a second list; now enumerate the second list and remove those items from the first (if you see what I mean)