C# 为什么我会得到一个残疾手术例外?

C# 为什么我会得到一个残疾手术例外?,c#,winforms,invalidoperationexception,C#,Winforms,Invalidoperationexception,可能是因为我同时更改了列表项吗? 如何防止该错误发生?在对集合进行枚举时,不能更改集合。创建集合的克隆并更改它。在对集合进行枚举时,不能更改集合。创建集合的克隆并更改它。这是因为您试图修改集合入侵者照片 foreach (var shotItem in Invadershots)// it points to me to there and doesnt allow me to loop.."{"Collection was modified; enumeration operation may

可能是因为我同时更改了列表项吗?

如何防止该错误发生?

在对集合进行枚举时,不能更改集合。创建集合的克隆并更改它。

在对集合进行枚举时,不能更改集合。创建集合的克隆并更改它。

这是因为您试图修改集合
入侵者照片

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);
  }
}

这在foreach中是不允许的,请改为使用..

这是因为您试图修改集合
入侵照片

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);
  }
}

这在foreach中是不允许的,请改用..

您不能这样做,将元素删除到列表中,您在foreach中读取的元素将崩溃,当然,在foreach中尝试创建一个副本以删除该元素,或者创建一个for迭代并正确控制元素的de数量和out条件


再见

删除列表中的元素是不可能的,在foreach中读取的元素会崩溃,当然,在foreach中尝试创建一个副本以删除该元素,或者创建一个for迭代并正确控制元素的数量和out条件


再见

哦,好吧,我明白了..该死。。我不知道哈图哦,好吧,我明白了…该死。。我不知道这顶帽子