【发布时间】:2011-01-02 17:02:01
【问题描述】:
我正在尝试从 ArrayList 中删除一个项目,但我得到了这个异常:Collection was modified; enumeration operation may not execute.
有什么想法吗?
【问题讨论】:
-
作为记录,除非您使用 .NET 1.0 或 1.1,否则您可能应该使用
List<T>而不是ArrayList。 -
为什么这个重复我在
Application.Exit();上有时收到这个错误,我没有操作任何集合或应用程序。 -
foreach(ItemCollection item in ItemCollection.Values) To foreach(ItemCollection s in ItemCollection.Values.ToList()) 问题是在 foreach 循环中正在修改 ItemCollection.Values。调用 ItemCollection.Values.ToList() 将subscribers.Values 的值复制到foreach 开头的单独列表中
标签: c# exception collections enumeration