【发布时间】:2011-03-01 15:46:41
【问题描述】:
可能的重复:
Exception during iteration on collection and remove items from that collection
How to remove elements from a generic list while iterating around it?
Better way to remove matched items from a list
// tmpClientList is List<Client> type
if (txtboxClientName.Text != "")
foreach (Client cli in tmpClientList)
if (cli.Name != txtboxClientName.Text)
tmpClientList.Remove(cli);
错误:“集合已修改;枚举操作可能无法执行。”
如何以某种简单的方式从列表中删除项目,而无需将这些项目的索引保存在另一个列表或数组中,并将它们删除到代码中的另一个位置。也试过 RemoveAt(index) 但情况完全相同,在循环运行时进行修改。
【问题讨论】:
-
同意完全重复的问题,见链接一stackoverflow.com/questions/1154325/…和链接二:stackoverflow.com/questions/1541777/…
标签: c#