Collection was modified; enumeration operation may not execute

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

    When a subscriber unsubscribes you are changing contents of the collection of Subscribers during enumeration.

    There are several ways to fix this, one being changing the for loop to use an explicit .ToList():

    public void NotifySubscribers(DataRecord sr)  
    {
        foreach(Subscriber s in subscribers.Values.ToList())
        {
                                                  ^^^^^^^^^  
            ...
    

提交回复
热议问题