c# - Removing an Item in a list -
this question has answer here:
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
Post a Comment