title: Intelligent way of removing items from a List<T> while enumerating in C#
body: <p>I have the classic case of trying to remove an item from a collection while enumerating it in a loop:</p>
  
  <pre><code>List&lt;int&gt; myIntCollection = new List&lt;int&gt;();
  myIntCollection.Add(42);
  myIntCollection.Add(12);
  myIntCollection.Add(96);
  myIntCollection.Add(25);
  
  foreach (int i in myIntCollection)
  {
      if (i == 42)
          myIntCollection.Remove(96);    // The error is here.
      if (i == 25)
          myIntCollection.Remove(42);    // The error is here.
  }
  </code></pre>
  
  <p>At the beginning of the iteration after a change takes place, an <code>InvalidOperationException</code> is thrown, because enumerators don’t like when the underlying collection changes.</p>
  
  <p>I need to make changes to the collection while iterating. There are many <strong>patterns that can be used to avoid this</strong>, but none of them seems to have a good solution:</p>
  
  <ol>
  <li><p>Do not delete inside this loop, instead keep a separate “Delete List”, that you process after the main loop.  </p>
  
  <p>This is normally a good solution, but in my case, I need the item to be gone instantly as “waiting” till after 
  the main loop to really delete the item changes the logic flow of my code.</p></li>
  <li><p>Instead of deleting the item, simply set a flag on the item and mark it as inactive. Then add the functionality of pattern 1 to clean up the list.  </p>
  
  <p>This <em>would</em> work for all of my needs, but it means that a <em>lot</em> of code will have to change in order to check the inactive flag every time an item is accessed. This is far too much administration for my liking.</p></li>
  <li><p>Somehow incorporate the ideas of pattern 2 in a class that derives from <code>List&lt;T&gt;</code>. This Superlist will handle the inactive flag, the deletion of objects after the fact and also will not expose items marked as inactive to enumeration consumers. Basically, it just encapsulates all the ideas of pattern 2 (and subsequently pattern 1).  </p>
  
  <p>Does a class like this exist? Does anyone have code for this? Or is there a better way?</p></li>
  <li><p>I’ve been told that accessing <code>myIntCollection.ToArray()</code> instead of <code>myIntCollection</code> will solve the problem and allow me to delete inside the loop.  </p>
  
  <p>This seems like a bad design pattern to me, or maybe it’s fine?</p></li>
  </ol>
  
  <p>Details:</p>
  
  <ul>
  <li><p>The list will contain many items and I will be removing only some of them.</p></li>
  <li><p>Inside the loop, I will be doing all sorts of processes, adding, removing etc., so the solution needs to be fairly generic.</p></li>
  <li><p>The item that I need to delete <em>may not</em> be the current item in the loop. For example, I may be on item 10 of a 30 item loop and need to remove item 6 or item 26. Walking backwards through the array will no longer work because of this. ;o(</p></li>
  </ul>