what is the most performant way to delete elements from a List<T> in c# while doing a foreach?
<p>i want to know what is the best way to delete elements from a List in c# while doing a foreach.</p>

<p>here is a code sample. first i create a list with some elements and then delte one:</p>

<pre><code>List&lt;int&gt; foo = new List&lt;int&gt;();
foo.Add(1);
foo.Add(2);
foo.Add(3);
foreach (int i in foo)
{
    if (i==2)
    {
        foo.Remove(i);
    }
}
</code></pre>

<p>when i run this, i get a <code>InvalidOperationException</code> but how to solve this with a performant way?</p>
