Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best solution is usually to use the <a href="http://www.google.ca/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBcQFjAA&amp;url=http://msdn.microsoft.com/en-us/library/wdka673a.aspx&amp;rct=j&amp;q=removeall%20msdn&amp;ei=Km5WTqv9Jenh0QHG_f2kDA&amp;usg=AFQjCNFYUfX-eMDHsuprtvWJy8Sdvvs0EA&amp;sig2=KX9Y1vppeBpmNWTsILfDkQ&amp;cad=rja"><code>RemoveAll()</code></a> method:</p> <pre><code>myList.RemoveAll(x =&gt; x.SomeProp == "SomeValue"); </code></pre> <p>Or, if you need <em>certain</em> elements removed:</p> <pre><code>MyListType[] elems = new[] { elem1, elem2 }; myList.RemoveAll(x =&gt; elems.Contains(x)); </code></pre> <p>This assume that your loop is solely intended for removal purposes, of course. If you <em>do</em> need to additional processing, then the best method is usually to use a <code>for</code> or <code>while</code> loop, since then you're not using an enumerator:</p> <pre><code>for (int i = myList.Count - 1; i &gt;= 0; i--) { // Do processing here, then... if (shouldRemoveCondition) { myList.RemoveAt(i); } } </code></pre> <p>Going backwards ensures that you don't skip any elements.</p> <p><strong>Response to Edit</strong>:</p> <p>If you're going to have seemingly arbitrary elements removed, the easiest method might be to just keep track of the elements you want to remove, and then remove them all at once after. Something like this:</p> <pre><code>List&lt;int&gt; toRemove = new List&lt;int&gt;(); foreach (var elem in myList) { // Do some stuff // Check for removal if (needToRemoveAnElement) { toRemove.Add(elem); } } // Remove everything here myList.RemoveAll(x =&gt; toRemove.Contains(x)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload