如果用foreach,会造成被遍历的集合更改后带来异常问题。

此时,用for循环可有效的解决这个问题。

for(int i=0;i<List.Count;i++)
{
 if(条件是真)
 {
 List.Remove(List[i]);
 i--;
 }
}

或者,再用另外的一个List集合存储要删除的对象。

List<T> newlists=new List<T>();
foreach(T t in List)
{
 lists.add(t);
}
foreach(T t in newlists)
{
 List.Remove(t);
}

相关文章:

  • 2021-07-31
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
  • 2022-12-23
  • 2022-01-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2022-01-03
相关资源
相似解决方案