title: C# - Collection was modified; enumeration operation may not execute
body: <p>There's a  button click event in a lengthy code I've written. I have a list of objects in it. Each time the button is clicked, the list should be modified ( for example, some items should be removed ) and then iterated using a foreach loop.</p>
  
  <pre><code>List&lt;Person&gt; lp=new List&lt;Person&gt;();
  lp.RemoveAt(2);
  
  foreach(Person j in lp)
  {
      // do something
  }
  </code></pre>
  
  <p>When I try to execute the above code, it results in an exception.</p>
  
  <p><strong>InvalidOperationException : Collection was modified; enumeration operation may not execute.</strong></p>
  
  <p>I found some solutions on the internet and tried them. One of them is,</p>
  
  <pre><code>foreach(Person j in lp.ToList())
  {
      // do something
  }
  </code></pre>
  
  <p>But nothing could stop the exception.</p>
  
  <p>can someone help with this ?</p>