InvalidOperationException. An InvalidOperationException has occurred. It reports a "collection was modified" error in the program. You have a collection such as a TreeView. You are removing items such as TreeNode controls in a foreach-loop.

Example. First let's examine some situations that can cause this exception, and then some ways to fix it. The example below demonstrates a TreeNodeCollection and a foreach loop that tries to remove an item, but raises an exception.

In this example, we use the Nodes property on the TreeNode type. This returns a collection of TreeNode objects that are nested in the TreeView. The foreach-loop iterates over each item in the Nodes collection.

Then: We call the Remove method, and it tries to modify the collection during the loop.

Error. The important part of the InvalidOperationException means is the Message property. The Message on the second line is the secret we need to know. The message says "Collection was modified" and that the enumeration won't work.

Note: We are changing the elements in the collection while looping over it with foreach.

Foreach queries the enumerator and asks for the next element. In our example, the enumerator's state becomes invalid when we remove the item. An enumerator has to store some data indicating where its current position.

In this example, we store a List of TreeNode objects separately. We built this up with the TreeNode controls that we wish to remove. We want to remove TreeNode controls with no children. It is just solid, working code—the best kind.

Summary. By itself, an InvalidOperationException is not useful, but the "collection was modified" error message can be helpful. To remove items from a collection, first add references to all the objects you want to remove in a new List.

Then: Remove those items when iterating over the temporary collection. This depends on the collection type.