my6solutions

asp .net, the social web & other distractions

 

Running Apps


PayPal - The safer, easier way to pay online!

Disclaimer

I am in no way affiliated with Microsoft or Google. I am just another developer trying to make a difference. All opinions and observations are usually my own.

Collection was modified; enumeration operation may not execute

That's the error you get when trying to remove an entry from an enumeratiion while traversing through it. For example, the following code snippet will generate the above error.

    1 foreach (String missing in unsupported)

    2 {

    3     if (!features[missing].getRequired())

    4     {

    5         unsupported.Remove(missing);

    6     }

    7 }

where unsopported is actually a HashSet of strings. There are numerous ways to solve this issue like using a temporary list to store entries that needs to be removed, to use a for loop and iterate backwards while removing matching entries. But the solution I like best is using lambda expressions like so.

    1 unsupported.RemoveWhere(x => !features[x].getRequired());


Bookmark and Share

Tags:
Categories: ASP .NET
Permalink | Comments (0) | Post RSSRSS comment feed