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 list.
You can't use .Remove(element) inside a foreach (var element in X)... you also can't use for (int i = 0; i < elements.Count(); i++) and .RemoveAt(i).
Previously I have done crazy things like this (summary = building a generic structure around the 'element' to hold a true or false value which determines whether or not it should be removed from the list):
public List<String> AttemptToProcessList(List<string> list) {
var listTracked = new List<KeyValuePair<string, bool>>();
list.ForEach(item => listTracked.Add(new KeyValuePair<string, bool>(item, false)));
for (int i = 0; i < listTracked.Count; i++) {
var result = ProcessListItem(listTracked[i].Key);
if (result) {
listTracked[i] = new KeyValuePair<string, bool>(listTracked[i].Key, true);
}
}
foreach (var item in listTracked.Where( listItem => listItem.Value )) {
list.Remove(item.Key);
}
return list;
}