I am looking for a better pattern for working with a list of elements which each need processed and then depending on the outcome are removed from
The best way to remove items from a list while iterating over it is to use RemoveAll()
. But the main concern written by people is that they have to do some complex things inside the loop and/or have complex compare cases.
The solution is to still use RemoveAll()
but use this notation:
var list = new List(Enumerable.Range(1, 10));
list.RemoveAll(item =>
{
// Do some complex operations here
// Or even some operations on the items
SomeFunction(item);
// In the end return true if the item is to be removed. False otherwise
return item > 5;
});