<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Kirk Jackson's Page of Words - CSharpTips</title>
    <link>http://pageofwords.com/blog/</link>
    <description>Run the ink across this page of words</description>
    <language>en-us</language>
    <copyright>Kirk Jackson</copyright>
    <lastBuildDate>Thu, 23 Oct 2008 11:08:06 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 1.9.6264.0</generator>
    <managingEditor>kirkj@paradise.net.nz</managingEditor>
    <webMaster>kirkj@paradise.net.nz</webMaster>
    <item>
      <trackback:ping>http://pageofwords.com/blog/Trackback.aspx?guid=1332f30e-9095-4ee4-b484-a2d1bfdcc4b9</trackback:ping>
      <pingback:server>http://pageofwords.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://pageofwords.com/blog/PermaLink,guid,1332f30e-9095-4ee4-b484-a2d1bfdcc4b9.aspx</pingback:target>
      <dc:creator>Kirk Jackson</dc:creator>
      <wfw:comment>http://pageofwords.com/blog/CommentView,guid,1332f30e-9095-4ee4-b484-a2d1bfdcc4b9.aspx</wfw:comment>
      <wfw:commentRss>http://pageofwords.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=1332f30e-9095-4ee4-b484-a2d1bfdcc4b9</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A common scenario is wanting to delete an item while iterating over a collection using
foreach:
</p>
        <div style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: courier new;">
          <p style="margin: 0px;">
            <span style="color: blue;">foreach</span> (<span style="color: blue;">string</span> name <span style="color: blue;">in</span> names)
</p>
          <p style="margin: 0px;">
{
</p>
          <p style="margin: 0px;">
    <span style="color: blue;">if</span> (name == <span style="color: rgb(163, 21, 21);">"Kirk"</span>)
</p>
          <p style="margin: 0px;">
    {
</p>
          <p style="margin: 0px;">
        names.Remove(name);
</p>
          <p style="margin: 0px;">
    }
</p>
          <p style="margin: 0px;">
}
</p>
        </div>
        <p>
Unfortunately, this will give the dreaded InvalidOperationException:
</p>
        <blockquote>
          <p>
Collection was modified; enumeration operation may not execute.
</p>
        </blockquote>
        <p>
This is because modifying the state of a collection invalidates the enumerator that <strong>foreach</strong> uses
behind the scenes to loop over the collection.
</p>
        <p>
A common work-around is to convert the <strong>foreach</strong> to a <strong>for</strong> loop:
</p>
        <div style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: courier new;">
          <p style="margin: 0px;">
            <span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = names.Count
- 1; i &gt;= 0; i--)
</p>
          <p style="margin: 0px;">
{
</p>
          <p style="margin: 0px;">
    <span style="color: blue;">if</span> (names[i] == <span style="color: rgb(163, 21, 21);">"Kirk"</span>)
</p>
          <p style="margin: 0px;">
    {
</p>
          <p style="margin: 0px;">
        names.RemoveAt(i);
</p>
          <p style="margin: 0px;">
    }
</p>
          <p style="margin: 0px;">
}
</p>
        </div>
        <p>
You'll notice that the for loop goes <em>backwards</em> from the end of the list (position:
Count - 1) back to the start (position: 0), so that when an item is removed, our current
index i is still a valid position in the list.
</p>
        <p>
How do you do it?
</p>
        <p>
Kirk
</p>
        <p>
Source (such that it is) <a href="http://pageofwords.com/blog/content/binary/WindowsLiveWriter/CTipRemovemodifyordeleteitemwithinaforea_1508A/Program.cs" target="_blank">Program.txt</a></p>
        <img width="0" height="0" src="http://pageofwords.com/blog/aggbug.ashx?id=1332f30e-9095-4ee4-b484-a2d1bfdcc4b9" />
      </body>
      <title>C# Tip: Remove, modify or delete item within a foreach</title>
      <guid isPermaLink="false">http://pageofwords.com/blog/PermaLink,guid,1332f30e-9095-4ee4-b484-a2d1bfdcc4b9.aspx</guid>
      <link>http://pageofwords.com/blog/2008/10/23/CTipRemoveModifyOrDeleteItemWithinAForeach.aspx</link>
      <pubDate>Thu, 23 Oct 2008 11:08:06 GMT</pubDate>
      <description>&lt;p&gt;
A common scenario is wanting to delete an item while iterating over a collection using
foreach:
&lt;/p&gt;
&lt;div style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: courier new;"&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue;"&gt;string&lt;/span&gt; name &lt;span style="color: blue;"&gt;in&lt;/span&gt; names)
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (name == &lt;span style="color: rgb(163, 21, 21);"&gt;"Kirk"&lt;/span&gt;)
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; names.Remove(name);
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Unfortunately, this will give the dreaded InvalidOperationException:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Collection was modified; enumeration operation may not execute.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
This is because modifying the state of a collection invalidates the enumerator that &lt;strong&gt;foreach&lt;/strong&gt; uses
behind the scenes to loop over the collection.
&lt;/p&gt;
&lt;p&gt;
A common work-around is to convert the &lt;strong&gt;foreach&lt;/strong&gt; to a &lt;strong&gt;for&lt;/strong&gt; loop:
&lt;/p&gt;
&lt;div style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: courier new;"&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;for&lt;/span&gt; (&lt;span style="color: blue;"&gt;int&lt;/span&gt; i = names.Count
- 1; i &amp;gt;= 0; i--)
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (names[i] == &lt;span style="color: rgb(163, 21, 21);"&gt;"Kirk"&lt;/span&gt;)
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; names.RemoveAt(i);
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
You'll notice that the for loop goes &lt;em&gt;backwards&lt;/em&gt; from the end of the list (position:
Count - 1) back to the start (position: 0), so that when an item is removed, our current
index i is still a valid position in the list.
&lt;/p&gt;
&lt;p&gt;
How do you do it?
&lt;/p&gt;
&lt;p&gt;
Kirk
&lt;/p&gt;
&lt;p&gt;
Source (such that it is) &lt;a href="http://pageofwords.com/blog/content/binary/WindowsLiveWriter/CTipRemovemodifyordeleteitemwithinaforea_1508A/Program.cs" target="_blank"&gt;Program.txt&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://pageofwords.com/blog/aggbug.ashx?id=1332f30e-9095-4ee4-b484-a2d1bfdcc4b9" /&gt;</description>
      <comments>http://pageofwords.com/blog/CommentView,guid,1332f30e-9095-4ee4-b484-a2d1bfdcc4b9.aspx</comments>
      <category>.NET;CSharpTips</category>
    </item>
  </channel>
</rss>