Efficiently deleting item from within 'foreach'

后端 未结 5 1244
别跟我提以往
别跟我提以往 2020-12-05 20:00

For now, the best I could think of is:

bool oneMoreTime = true;
while (oneMoreTime)
{
    ItemType toDelete=null;
    oneMoreTime=false;
    foreach (ItemTyp         


        
5条回答
  •  长情又很酷
    2020-12-05 20:29

    Just as I finished typing I remembered that there is lambda-way to do it.

    collection.RemoveAll(i=>ShouldBeDeleted(i));
    

    Better way?

提交回复
热议问题