Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection was modified; enumeration operation may not execute - why?

I'm enumerating over a collection that implements IList, and during the enumeration I am modifying the collection. I get the error, "Collection was modified; enumeration operation may not execute."

I want to know why this error occurs when modifying a item in the collection during iteration. I've already converted my foreach loop to a for loop, but I want to know the 'details' on why this error occurs.

like image 371
contactmatt Avatar asked Dec 27 '10 00:12

contactmatt


People also ask

Why do I get system invalidoperationexception collection was modified?

System.InvalidOperationException: Collection was modified; enumeration operation may not execute. This error can happen in two scenarios: You’re looping over the collection in a foreach loop and modifying it (add/removing) in the same loop.

Is it possible to enumerate and modify a collection at the same time?

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.

What is collection was modified error in C?

Why the error Collection was modified; enumeration operation may not execute occurs and how to handle it in C#? This error occurs when a looping process is being running on a collection (Ex: List) and the collection is modified (data added or removed) during the runtime.

How to enumerate and modify a collection in a foreach block?

You cannot enumerate a collection in a foreach block and modify the collection in the same block. Usually the solution is to get back to a plain old index iteration, starting from the end of the collection. In clear, change Please Sign up or sign in to vote. after the foreach loop you can then remove the items in toRemove from account_Funds.


1 Answers

From the IEnumerable documentation:

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined.

I believe the reasoning for this decision is that it cannot be guaranteed that all types of collections can sustain modification and still preserve an enumerator state. Consider a linked list -- if you remove a node and an enumerator is currently on that node, the node reference may be its only state. And once that node is removed, the "next node" reference will be set to null, effectively invalidating the enumerator state and preventing further enumeration.

Since some collection implementations would have serious trouble with this kind of situation, it was decided to make this part of the IEnumerable interface contract. Allowing modification in some situations and not others would be horribly confusing. In addition, this would mean that existing code that might rely on modifying a collection while enumerating it would have serious problems when the collection implementation is changed. So making the behavior consistent across all enumerables is preferable.

like image 128
cdhowie Avatar answered Oct 12 '22 17:10

cdhowie