
You encountered an InvalidOperationException that reports a "collection was modified" error in your program. You have a collection such as a TreeView, and you are removing items such as TreeNode controls in a foreach loop. Here we see ways you can solve this exception in your C# programs.
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.
TreeView TutorialThis C# example provokes InvalidOperationException. It shows the collection was modified error.
Code that causes exception [C#]
foreach (TreeNode treeNode in treeView1.Nodes)
{
if (treeNode.Nodes.Count == 0)
{
treeNode.Remove();
}
}
Exception output
System.InvalidOperationException was unhandled
Message="Collection was modified; enumeration operation may not execute."
Source="mscorlib"
StackTrace:
at System.ThrowHelper.ThrowInvalidOperationException(...)
at System.Collections.Generic.List`1.Enumerator.MoveNext()Description. It uses the Nodes collection. This is a collection of TreeNode objects that are nested in the TreeView. The foreach loop iterates over each item in the Nodes collection. 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. This is because we are changing the elements in the collection while looping over it with foreach.

What foreach does. It 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 it currently is.
Foreach Loop ExamplesNext, let's fix the problematic code with some changes. This problem often occurs in Windows Forms control collections. I was puzzled by it when trying to remove nodes from a TreeView control. What follows is code that accomplished the goal in the first example, without raising an exception.
Code that fixes exception [C#]
List<TreeNode> treeList = new List<TreeNode>();
foreach (TreeNode treeNode in treeView1.Nodes)
{
if (treeNode.Nodes.Count == 0)
{
treeList.Add(treeNode);
}
}
foreach (TreeNode treeNode in treeList)
{
treeNode.Remove();
}
Description. We can store a List of TreeNode objects separately. We built this up with the TreeNode controls that we wish to remove. In the example, we want to remove TreeNode controls with no children. It is just solid, working code—the best kind.
List Examples
By itself, an InvalidOperationException is not useful, but the "collection was modified" error message can be helpful. To remove items from a collection, such as List or TreeNodeCollection, 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.
Exception Handling