correct way of looping through a list and remove items
<p>I wrote a function to go through a list and remove list items if some conditions where met. My program crashed on it, and after a while i concluded that the outer for loop, goes through all items in the list.
While at the same routine the list of item can get shorter.</p>

<pre><code> // Lijst is a list of a struct that contains a value .scanned and .price

 for (int i = 0; i &lt; Lijst.Count; i++)
  {
   if (Lijst[i].scanned == false)
    {
     // (removed deletion of list item i here)
     if (Lijst[i].price &gt; (int)nudMinimum.Value)
      {
       Totaal++;
        lblDebug.Text = Totaal.ToString();
      }
    Lijst.RemoveAt(i); //&lt;-moved to here
    } 
  }
</code></pre>

<p>Now i wonder whats the correct to do this, without getting index out of range errors.</p>