c# - Removing an Item in a list -


i'm programming in c#. i'm trying remove item list<> when remove item exception error:

an exception of type 'system.invalidoperationexception' occurred in mscorlib.dll not handled in user code

additional information: collection modified; enumeration operation may not execute.

here code:

foreach (target t in targetlist) {     if (t.calculatedistance(t.endx, t.endy) <= 5)     {         targetlist.remove(t);     } } 

i exception on first line. why see error? or how can fix it?

the problem face can't modify collection itterate thru. solve using linq:

targetlist.removeall(t => t.calculatedistance(t.endx, t.endy) <= 5); 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -