How to remove all elements from a dictionary?

A much simpler (and much more efficient) approach:

taggings.Clear();

and yes, the error is because changing the data deliberately breaks iterators.


Try using the Clear method instead.

internal static void RemoveAllSourceFiles()
        {
           taggings.Clear();
        }

Update: And as Marc pointed out, you cannot continue iterating over a collection while you modify it because the iterator is irrecoverably invalidated. Please read the answer to this SO question for details.

Why does enumerating through a collection throw an exception but looping through its items does not


Dictionary.Clear?

Tags:

C#

.Net