In my application, _collection is a List from which I need to remove all User objects which do not match the criteria.
However, the following code gets an invalid operation error in its second iteration since the _collection itself has been changed:
foreach (User user in _collection)
{
if (!user.IsApproved())
{
_collection.Remove(user);
}
}
I could create another List collection and copy them back and forth but then I have the issue of non-cloned reference types, etc.
Is there a way to do the above more elegantly than copying _collection to another another List variable?