Getting InvalidOperationException when modifying the collection while loopng through it
<p>I have a collection lets say list of integers and looping through it using foreach, but if I have to insert data into the list during the loop, how do I do that ? I'm getting </p>

<blockquote>
  <p>Unhandled Exception: System.InvalidOperationException: Collection was
  modified enumeration operation may not execute.</p>
</blockquote>

<p>This is the sample code below. I thought AsReadonly() would return a collection that was prior to modified state. That is not the case here.</p>

<pre><code>ints = new List&lt;int&gt;(30);
ints.AddRange(Enumerable.Range(1, 10));
int y = 11;

foreach(int x in ints.AsReadOnly())
{
    ints.Add(y++);
    Console.WriteLine(x);
}
</code></pre>
