Collection was modified; enumeration operation may not execute

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

    A more efficient way, in my opinion, is to have another list that you declare that you put anything that is "to be removed" into. Then after you finish your main loop (without the .ToList()), you do another loop over the "to be removed" list, removing each entry as it happens. So in your class you add:

    private List<Guid> toBeRemoved = new List<Guid>();
    

    Then you change it to:

    public void NotifySubscribers(DataRecord sr)
    {
        toBeRemoved.Clear();
    
        ...your unchanged code skipped...
    
       foreach ( Guid clientId in toBeRemoved )
       {
            try
            {
                subscribers.Remove(clientId);
            }
            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Unsubscribe Error " + 
                    e.Message);
            }
       }
    }
    
    ...your unchanged code skipped...
    
    public void UnsubscribeEvent(Guid clientId)
    {
        toBeRemoved.Add( clientId );
    }
    

    This will not only solve your problem, it will prevent you from having to keep creating a list from your dictionary, which is expensive if there are a lot of subscribers in there. Assuming the list of subscribers to be removed on any given iteration is lower than the total number in the list, this should be faster. But of course feel free to profile it to be sure that's the case if there's any doubt in your specific usage situation.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 06:43

    Okay so what helped me was iterating backwards. I was trying to remove an entry from a list but iterating upwards and it screwed up the loop because the entry didn't exist anymore:

    for (int x = myList.Count - 1; x > -1; x--)
    {
        myList.RemoveAt(x);
    }
    
    0 讨论(0)
  • 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())
        {
                                                  ^^^^^^^^^  
            ...
    
    0 讨论(0)
  • 2020-11-21 06:48

    Why this error?

    In general .Net collections do not support being enumerated and modified at the same time. If you try to modify the collection list during enumeration, it raises an exception. So the issue behind this error is, we can not modify the list/dictionary while we are looping through the same.

    One of the solutions

    If we iterate a dictionary using a list of its keys, in parallel we can modify the dictionary object, as we are iterating through the key-collection and not the dictionary(and iterating its key collection).

    Example

    //get key collection from dictionary into a list to loop through
    List<int> keys = new List<int>(Dictionary.Keys);
    
    // iterating key collection using a simple for-each loop
    foreach (int key in keys)
    {
      // Now we can perform any modification with values of the dictionary.
      Dictionary[key] = Dictionary[key] - 1;
    }
    

    Here is a blog post about this solution.

    And for a deep dive in StackOverflow: Why this error occurs?

    0 讨论(0)
  • 2020-11-21 06:50

    Actually the problem seems to me that you are removing elements from the list and expecting to continue to read the list as if nothing had happened.

    What you really need to do is to start from the end and back to the begining. Even if you remove elements from the list you will be able to continue reading it.

    0 讨论(0)
提交回复
热议问题