I have a List that i want to delete some members from. I want to use this kind of syntax :
List<Object> Foo = new List<Object>();
//Some code
foreach (Object obj in Foo)
if (obj.ShouldBeDeleted())
Foo.Remove(obj);
It felt wrong to iterate over a list and modify it at the same time, and a quick test confirmed it with an InvalidOperationException.
What is the correct way to selectively remove members of a list? I thought of using a copy of the list, and iterate over the copy while I delete from the original list, but it does not feel very memory efficient.