【发布时间】:2011-11-01 21:15:57
【问题描述】:
有一些类对象的典型集合,例如string 为简单起见
IList<string> collection = new List<string>();
在程序流程中,一些函数需要对集合进行操作,处理类对象,并根据操作结果从集合中删除处理过的类对象。
例如以下字符串示例如果字符串“失败”则删除该项目
foreach (string str in collection)
{
// operate on the current class object
// if str is not valid
if (str == "failed")
collection.Remove(str);
}
无论如何都会导致异常。 遍历所有能够在枚举期间删除它的元素的最佳方法是什么?
【问题讨论】:
标签: c# .net collections enumeration