title: C# loop where the collection is modified
body: <p>Have a loop like this:</p>
  
  <pre><code>List&lt;Category&gt; categories = new List&lt;Category&gt;();
  
  foreach(Category category in categories)
  {
      ...
  
      if(...)
      {
          Category subcategory = new Category();
  
          ...
  
          categories.Add(subcategory);
      }
  }
  </code></pre>
  
  <p>Problem is of course that the loop collection (categories) doesn't like to be modified while it's running, but I need to do the exact same things to the subcategories as I do to the categories I have to begin with.</p>
  
  <p>Is the only option to do like this - or is there a smarter way to do it?</p>
  
  <pre><code>List&lt;Category&gt; categories = new List&lt;Category&gt;();
  List&lt;Category&gt; subcategories = new List&lt;Category&gt;();
  
  foreach(Category category in categories)
  {
      ...
  
      if(...)
      {
          Category subcategory = new Category();
  
          ...
  
          subcategories.Add(subcategory);
      }
  }
  
  foreach(Category subcategory in subcategories)
  {
      ...
  }
  </code></pre>
  
  <p>Thanks.</p>