There is a typical collection of some class objects e.g. string
for a simplicity
IList<string> collection = new List<string>();
During the program flow some function needs to operate on the collection, process class object and depending on the result of operation remove processed class object from collection.
E.g. following example of strings if string is "failed" then remove that item
foreach (string str in collection)
{
// operate on the current class object
// if str is not valid
if (str == "failed")
collection.Remove(str);
}
By all means that leads to exception. What is the best way to loop thru all elements having ability to remove it during enumeration?