foreach (var shotItem in Invadershots)// it points to me to there and doesnt allow me to loop.."{"Collection was modified; enumeration operation may not execute."}"
{
shotItem.Move();// it happens when this simple method called (which actually checks some bool..if the shot was out of the winform).
if (shotItem.removeShot)
{
Invadershots.Remove(shotItem);
}
}
可能是因为我同时更改列表项吗?
我如何防止该错误发生?
最佳答案
您无法在枚举整个集合的同时更改其集合。创建集合的一个副本并对其进行更改。
关于c# - 为什么会收到InvalidOperationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5675407/