How to remove elements from a generic list while iterating over it?

前端 未结 27 2377
忘了有多久
忘了有多久 2020-11-21 22:48

I am looking for a better pattern for working with a list of elements which each need processed and then depending on the outcome are removed from

27条回答
  •  醉酒成梦
    2020-11-21 23:23

    By assuming that predicate is a Boolean property of an element, that if it is true, then the element should be removed:

            int i = 0;
            while (i < list.Count())
            {
                if (list[i].predicate == true)
                {
                    list.RemoveAt(i);
                    continue;
                }
                i++;
            }
    

提交回复
热议问题