title: Remove/Add items to/from a list while iterating it
body: <p>First, I know this isn't possible out of the box because of obvious reasons.</p>
  
  <pre><code>foreach(string item in myListOfStrings) {
      myListOfStrings.Remove(item);
  }
  </code></pre>
  
  <p>The snipped above is one of the most horrible things I've ever seen. So, how do you achieve it then? You could iterate through the list backwards using <code>for</code>, but I don't like this solution either.</p>
  
  <p>What I'm wondering is: Is there a method/extensions that returns an IEnumerable from the current list, something like a floating copy? LINQ has numerous extension methods that do exactly this, but you always have to do something with it, such as filtering (where, take...).</p>
  
  <p>I'm looking forward to something like this:</p>
  
  <pre><code>foreach(string item in myListOfStrings.Shadow()) {
     myListOfStrings.Remove(item);
  }
  </code></pre>
  
  <p>where as .Shadow() is:</p>
  
  <pre><code>public static IEnumerable&lt;T&gt; Shadow&lt;T&gt;(this IEnumerable&lt;T&gt; source) {
      return new IEnumerable&lt;T&gt;(source);
      // or return source.Copy()
      // or return source.TakeAll();
  }
  </code></pre>
  
  <p><strong>Example</strong></p>
  
  <pre><code>foreach(ResponseFlags flag in responseFlagsList.Shadow()) {
      switch(flag) {
          case ResponseFlags.Case1:
              ...
          case ResponseFlags.Case2:
              ...
      }
      ...
      this.InvokeSomeVoidEvent(flag)
      responseFlagsList.Remove(flag);
  }
  </code></pre>
  
  <hr>
  
  <h2>Solution</h2>
  
  <p>This is how I solved it, and it works like a charm:</p>
  
  <pre><code>public static IEnumerable&lt;T&gt; Shadow&lt;T&gt;(this IEnumerable&lt;T&gt; source) where T: new() {
      foreach(T item in source)
          yield return item;
  }
  </code></pre>
  
  <p>It's not that super fast (obviously), but it's safe and exactly what I intended to do.</p>