我有我希望看起来像这样的代码:
List<Type> Os;
...
foreach (Type o in Os)
if (o.cond)
return; // Quitting early is important for my case!
else
Os.Remove(o);
... // Other code
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为当您foreach在该列表上的循环内时,无法从列表中删除:
有没有一种解决问题的常用方法?
如果需要,我可以切换到不同的类型.
选项2:
List<Type> Os;
...
while (Os.Count != 0)
if (Os[0].cond)
return;
else
Os.RemoveAt(0);
... // Other code
Run Code Online (Sandbox Code Playgroud)
丑陋,但它应该工作.