Remove items from a collection that matches a constraint

06May

If you want to remove some elements that are matching a constraint from a list, and you use a foreach loop to traverse the list you get the exception you will get an exception saying that you cannot change the collection during an enumeration of if you remove an item.

This is because you cannot modify a collection during enumerating it. Despite you cannot remove an item, you also could not add any item. This problem could be addressed by changing the foreach-loop with a for-loop