title: Add elements to a list while iterating over it
body: <p>I'm trying to add new elements to a list of lists while iterating over it</p>
  
  <pre><code>List&lt;List&lt;String&gt;&gt; sets = new List&lt;List&lt;string&gt;&gt;();
  
  foreach (List&lt;String&gt; list in sets)
  {
        foreach (String c in X)
        {
            List&lt;String&gt; newSet = ir_a(list, c, productions);
  
            if (newSet.Count &gt; 0)
            {
                sets.Add(newSet);
            }
        }
  }
  </code></pre>
  
  <p>The error I get after a few loops is this:</p>
  
  <pre><code>Collection was modified; enumeration operation may not execute
  </code></pre>
  
  <p>I know the error is caused by modifying the list, so my question is: What's the best or most fancy way to sort this thing out?</p>
  
  <p>Thanks</p>