Collection was modified; enumeration operation may not execute

后端 未结 16 3090
南旧
南旧 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:40

    This way should cover a situation of concurrency when the function is called again while is still executing (and items need used only once):

     while (list.Count > 0)
     {
        string Item = list[0];
        list.RemoveAt(0);
     
        // do here what you need to do with item
     
     } 
     
    

    If the function get called while is still executing items will not reiterate from the first again as they get deleted as soon as they get used. Should not affect performance much for small lists.

提交回复
热议问题