Collection was modified; enumeration operation may not execute

后端 未结 16 2613
南旧
南旧 2020-11-21 06:05

I can\'t get to the bottom of this error, because when the debugger is attached, it does not seem to occur.

Collection was modified; enumeration operatio

16条回答
  •  我寻月下人不归
    2020-11-21 06:55

    InvalidOperationException- An InvalidOperationException has occurred. It reports a "collection was modified" in a foreach-loop

    Use break statement, Once the object is removed.

    ex:

    ArrayList list = new ArrayList(); 
    
    foreach (var item in list)
    {
        if(condition)
        {
            list.remove(item);
            break;
        }
    }
    

提交回复
热议问题