• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How to get around the "collection has changed" BS in C#?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
Just off the top of my head, could you do something like this?
Code:
int i = 0;
while(i < list.length)
{
  if(list.wannaRemove(i))
  {
    list.remove(i);
  }
  else
    i++;
}

I'm no expert on C#, but I'd think it'd recalculate length when it goes back to the top of the while loop.

This is a case where you will get better performance and for that matter easier code by doing a reverse iteration over the list.

Code:
int i = list.length-1;
while(i >=0 )
{
  if(list.wannaRemove(i))
  {
    list.remove(i);
  }
  i--;
}

The reason being, depending on the management code, you will have less data to move in the large moves towards at beginning of the list as the higher items have already been removed.
 
Back
Top