Best practice to avoid InvalidOperationException: Collection was modified?

前端 未结 4 895
没有蜡笔的小新
没有蜡笔的小新 2021-02-08 00:42

Very often I need something like that:

 foreach (Line line in lines)
 {
    if (line.FullfilsCertainConditions())
    {
       lines.Remove(line)
    }
 }
         


        
4条回答
  •  梦谈多话
    2021-02-08 00:44

    You could simply replace original list with filtered one:

    lines = lines.Where(line => line.FullfilsCertainConditions()).ToList();
    

提交回复
热议问题