Can't add/remove items from a collection while foreach is iterating over it

前端 未结 4 1230
旧时难觅i
旧时难觅i 2020-12-10 19:21

If I make my own implementation of IEnumerator interface, then I am able ( inside foreach statement )to add or remove items from a albumsList

4条回答
  •  春和景丽
    2020-12-10 20:09

    Easiest way is to either reverse through the items like for(int i = items.Count-1; i >=0; i--), or loop once, gather all the items to remove in a list, then loop through the items to remove, removing them from the original list.

提交回复
热议问题