Collection enumeration exception

  • Thread starter Thread starter Nicholas Paldino [.NET/C# MVP]
  • Start date Start date
N

Nicholas Paldino [.NET/C# MVP]

Chuck,

Are you removing the item while you are enumerating through it? If so,
then this is not allowed. You need to cycle through the object using the
indexer, or whatever other access method there is to get an item from the
object.

Hope this helps.
 
I have a collection that I want to edit. When I remove an object from the
collection and then try to enumerate thru it with the foreach statement it
throws an exception - "Collection was modified; enumeration operation may
not execute."

Can somebody tell me how to fix this?
 
Thanks Nicholas. I feel like an idiot. Forgot the thing was in a big loop...
Whoops... :D

Nicholas Paldino said:
Chuck,

Are you removing the item while you are enumerating through it? If so,
then this is not allowed. You need to cycle through the object using the
indexer, or whatever other access method there is to get an item from the
object.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chuck Bowling said:
I have a collection that I want to edit. When I remove an object from the
collection and then try to enumerate thru it with the foreach statement it
throws an exception - "Collection was modified; enumeration operation may
not execute."

Can somebody tell me how to fix this?
 
Back
Top