为什么我会收到InvalidOperationException?

Dmi*_*iyd 1 c# invalidoperationexception winforms

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

可能是因为我同时更改了列表项吗?
如何防止发生错误?

And*_*ich 5

这是因为您尝试修改集合 Invadershots

Invadershots.Remove(shotItem);
Run Code Online (Sandbox Code Playgroud)

这在foreach中是不允许的,用于代替..