Collection was modified; enumeration operation may not execute

后端 未结 16 2576
南旧
南旧 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 07:04

    What's likely happening is that SignalData is indirectly changing the subscribers dictionary under the hood during the loop and leading to that message. You can verify this by changing

    foreach(Subscriber s in subscribers.Values)
    

    To

    foreach(Subscriber s in subscribers.Values.ToList())
    

    If I'm right, the problem will disappear.

    Calling subscribers.Values.ToList() copies the values of subscribers.Values to a separate list at the start of the foreach. Nothing else has access to this list (it doesn't even have a variable name!), so nothing can modify it inside the loop.

提交回复
热议问题