<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Collection was modified; enumeration operation may not execute</title>
	<atom:link href="http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/feed/" rel="self" type="application/rss+xml" />
	<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/</link>
	<description>Like electrons in the aether...</description>
	<lastBuildDate>Tue, 01 Dec 2009 16:14:46 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Bhavin</title>
		<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2356</link>
		<dc:creator>Bhavin</dc:creator>
		<pubDate>Thu, 08 Oct 2009 13:58:13 +0000</pubDate>
		<guid isPermaLink="false">http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2356</guid>
		<description>This is really helpful in my case. Thanks a lot for writing such a nice article.</description>
		<content:encoded><![CDATA[<p>This is really helpful in my case. Thanks a lot for writing such a nice article.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TEST</title>
		<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2342</link>
		<dc:creator>TEST</dc:creator>
		<pubDate>Wed, 15 Jul 2009 11:47:30 +0000</pubDate>
		<guid isPermaLink="false">http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2342</guid>
		<description>The Data Access Application Block component includes the Microsoft Visual Basic .NET source code or the Microsoft Visual Basic 2005 source code and the Microsoft Visual C# .NET source code or the Microsoft Visual C# 2005 source code for the Data Access Application Block. It also includes a Quick Start Samples client application in each language that you can use to test common scenarios. The sample can help you to better understand how the Data Access Application Block works. You can also customize the source code to fit your requirements.</description>
		<content:encoded><![CDATA[<p>The Data Access Application Block component includes the Microsoft Visual Basic .NET source code or the Microsoft Visual Basic 2005 source code and the Microsoft Visual C# .NET source code or the Microsoft Visual C# 2005 source code for the Data Access Application Block. It also includes a Quick Start Samples client application in each language that you can use to test common scenarios. The sample can help you to better understand how the Data Access Application Block works. You can also customize the source code to fit your requirements.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: luke</title>
		<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2325</link>
		<dc:creator>luke</dc:creator>
		<pubDate>Mon, 27 Apr 2009 22:23:06 +0000</pubDate>
		<guid isPermaLink="false">http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2325</guid>
		<description>Thanks mate, you made that really clear.</description>
		<content:encoded><![CDATA[<p>Thanks mate, you made that really clear.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: baby</title>
		<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2309</link>
		<dc:creator>baby</dc:creator>
		<pubDate>Wed, 25 Feb 2009 06:00:43 +0000</pubDate>
		<guid isPermaLink="false">http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2309</guid>
		<description>While I&#039;m working in calenders dayrender event, in dynamically creating a linkbutton this error occurs.

  if (e.Day.Date.Month == 1 &amp;&amp; e.Day.Date.Day == 26)
                        {
                            LinkButton Link = new LinkButton();
                            Link.Text = &quot;Event&quot;;
                            Link.ID = &quot;LinkButton1&quot;;
                            Link.Click += new EventHandler(LinkButton1_Click);
}</description>
		<content:encoded><![CDATA[<p>While I&#8217;m working in calenders dayrender event, in dynamically creating a linkbutton this error occurs.</p>
<p>  if (e.Day.Date.Month == 1 &amp;&amp; e.Day.Date.Day == 26)<br />
                        {<br />
                            LinkButton Link = new LinkButton();<br />
                            Link.Text = &#8220;Event&#8221;;<br />
                            Link.ID = &#8220;LinkButton1&#8243;;<br />
                            Link.Click += new EventHandler(LinkButton1_Click);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2307</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Thu, 19 Feb 2009 18:00:03 +0000</pubDate>
		<guid isPermaLink="false">http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2307</guid>
		<description>&quot;Calling “count” on an object is a far more expensive operation than just checking a variable, or iterating through a list.&quot;

is not true because upon closer inspection of the disassembly of List I noticed Count is simply a property with backing field that is evaulated in Add(), Remove(), RemoveAt() etc.

    public int Count
    {
        get
        {
            return this._size;
        }
    }</description>
		<content:encoded><![CDATA[<p>&#8220;Calling “count” on an object is a far more expensive operation than just checking a variable, or iterating through a list.&#8221;</p>
<p>is not true because upon closer inspection of the disassembly of List I noticed Count is simply a property with backing field that is evaulated in Add(), Remove(), RemoveAt() etc.</p>
<p>    public int Count<br />
    {<br />
        get<br />
        {<br />
            return this._size;<br />
        }<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom</title>
		<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2306</link>
		<dc:creator>Tom</dc:creator>
		<pubDate>Wed, 11 Feb 2009 17:15:46 +0000</pubDate>
		<guid isPermaLink="false">http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2306</guid>
		<description>Thanks Frater. I had the same problem and the explanation you did is great. But since I only need to delete one from the collection, using foreach loop will work. Just call the &quot;break&quot; statement after calling the Remove method. This will stop the iteration of the collection and error will not appear anymore. Thanks again.

foreach (string s in PacketList)
            {
                if (s.IndexOf(sTag) == 1)
                {
                    PacketList.Remove(s);
                    break;// without this the exception will be thrown
                }
            }</description>
		<content:encoded><![CDATA[<p>Thanks Frater. I had the same problem and the explanation you did is great. But since I only need to delete one from the collection, using foreach loop will work. Just call the &#8220;break&#8221; statement after calling the Remove method. This will stop the iteration of the collection and error will not appear anymore. Thanks again.</p>
<p>foreach (string s in PacketList)<br />
            {<br />
                if (s.IndexOf(sTag) == 1)<br />
                {<br />
                    PacketList.Remove(s);<br />
                    break;// without this the exception will be thrown<br />
                }<br />
            }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lakshmi</title>
		<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2305</link>
		<dc:creator>lakshmi</dc:creator>
		<pubDate>Mon, 09 Feb 2009 20:03:50 +0000</pubDate>
		<guid isPermaLink="false">http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2305</guid>
		<description>Thanks,
This is What I  was looking  for.</description>
		<content:encoded><![CDATA[<p>Thanks,<br />
This is What I  was looking  for.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bernard Vander Beken</title>
		<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2303</link>
		<dc:creator>Bernard Vander Beken</dc:creator>
		<pubDate>Fri, 23 Jan 2009 09:15:37 +0000</pubDate>
		<guid isPermaLink="false">http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2303</guid>
		<description>If you change the line
 foreach(bool b in booleanValues)
to
 foreach(bool b in booleanValues.ToArray())

then you should be able to remove the values from the list in this foreach loop (thanks to stackoverflow.com for this hint).</description>
		<content:encoded><![CDATA[<p>If you change the line<br />
 foreach(bool b in booleanValues)<br />
to<br />
 foreach(bool b in booleanValues.ToArray())</p>
<p>then you should be able to remove the values from the list in this foreach loop (thanks to stackoverflow.com for this hint).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2301</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Mon, 19 Jan 2009 14:36:27 +0000</pubDate>
		<guid isPermaLink="false">http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2301</guid>
		<description>You can always cache the result of  (booleanValues.Count - 1) and decrement though I did not notice a performance improvement by doing this.</description>
		<content:encoded><![CDATA[<p>You can always cache the result of  (booleanValues.Count &#8211; 1) and decrement though I did not notice a performance improvement by doing this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2299</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Fri, 16 Jan 2009 11:21:22 +0000</pubDate>
		<guid isPermaLink="false">http://frater.wordpress.com/2008/02/20/collection-was-modified-enumeration-operation-may-not-execute/#comment-2299</guid>
		<description>Sorry yes, you need to decrease i by 1 when an item is removed from the list to avoid skipping the next value.

            for (int i = 0; i &lt;= booleanValues.Count - 1; i++)
            {
                if (!booleanValues[i])
                {
                    booleanValues.RemoveAt(i);
                    i--;
                }
            }

One this is in place however the RemoveAt() method is much faster.  
Start a new a new console application and try this

My code took about 10 seconds and yours took nearly a minute and a half.

using System;
using System.Collections.Generic;

namespace LoopTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            DateTime startTime;
            List booleanValues;

            booleanValues = GetBooleanList();

            startTime = DateTime.Now;
            for (int i = 0; i &lt;= booleanValues.Count - 1; i++)
            {
                if (!booleanValues[i])
                {
                    booleanValues.RemoveAt(i);
                    i--;
                }
            }

            Console.WriteLine(string.Format(&quot;Bool Count = {0}, Time = {1}&quot;, booleanValues.Count,
                                          DateTime.Now.Subtract(startTime)));
            booleanValues = GetBooleanList();

            startTime = DateTime.Now;
            List valuesToDelete = new List();
            foreach (bool b in booleanValues)
            {
                if (!b)
                    valuesToDelete.Add(b);
            }
            foreach (bool b in valuesToDelete)
            {
                booleanValues.Remove(b);
            }

            Console.WriteLine(string.Format(&quot;Bool Count = {0}, Time = {1}&quot;, booleanValues.Count,
                                          DateTime.Now.Subtract(startTime)));
            Console.Write(&quot;\nPress any key to continue:&quot;); 
            Console.Read(); 
        }

        private static List GetBooleanList()
        {
            List values = new List();
            for (int i = 0; i &lt;= 100000; i++)
            {
                values.Add(true);
            }
            for (int i = 0; i &lt;= 100000; i++)
            {
                values.Add(false);
            }
            for (int i = 0; i &lt;= 100000; i++)
            {
                values.Add(true);
            }
            for (int i = 0; i &lt;= 100000; i++)
            {
                values.Add(false);
            }
            return values;
        }
    }
}</description>
		<content:encoded><![CDATA[<p>Sorry yes, you need to decrease i by 1 when an item is removed from the list to avoid skipping the next value.</p>
<p>            for (int i = 0; i &lt;= booleanValues.Count &#8211; 1; i++)<br />
            {<br />
                if (!booleanValues[i])<br />
                {<br />
                    booleanValues.RemoveAt(i);<br />
                    i&#8211;;<br />
                }<br />
            }</p>
<p>One this is in place however the RemoveAt() method is much faster.<br />
Start a new a new console application and try this</p>
<p>My code took about 10 seconds and yours took nearly a minute and a half.</p>
<p>using System;<br />
using System.Collections.Generic;</p>
<p>namespace LoopTest<br />
{<br />
    internal class Program<br />
    {<br />
        private static void Main(string[] args)<br />
        {<br />
            DateTime startTime;<br />
            List booleanValues;</p>
<p>            booleanValues = GetBooleanList();</p>
<p>            startTime = DateTime.Now;<br />
            for (int i = 0; i &lt;= booleanValues.Count &#8211; 1; i++)<br />
            {<br />
                if (!booleanValues[i])<br />
                {<br />
                    booleanValues.RemoveAt(i);<br />
                    i&#8211;;<br />
                }<br />
            }</p>
<p>            Console.WriteLine(string.Format(&quot;Bool Count = {0}, Time = {1}&quot;, booleanValues.Count,<br />
                                          DateTime.Now.Subtract(startTime)));<br />
            booleanValues = GetBooleanList();</p>
<p>            startTime = DateTime.Now;<br />
            List valuesToDelete = new List();<br />
            foreach (bool b in booleanValues)<br />
            {<br />
                if (!b)<br />
                    valuesToDelete.Add(b);<br />
            }<br />
            foreach (bool b in valuesToDelete)<br />
            {<br />
                booleanValues.Remove(b);<br />
            }</p>
<p>            Console.WriteLine(string.Format(&#8220;Bool Count = {0}, Time = {1}&#8221;, booleanValues.Count,<br />
                                          DateTime.Now.Subtract(startTime)));<br />
            Console.Write(&#8220;\nPress any key to continue:&#8221;);<br />
            Console.Read();<br />
        }</p>
<p>        private static List GetBooleanList()<br />
        {<br />
            List values = new List();<br />
            for (int i = 0; i &lt;= 100000; i++)<br />
            {<br />
                values.Add(true);<br />
            }<br />
            for (int i = 0; i &lt;= 100000; i++)<br />
            {<br />
                values.Add(false);<br />
            }<br />
            for (int i = 0; i &lt;= 100000; i++)<br />
            {<br />
                values.Add(true);<br />
            }<br />
            for (int i = 0; i &lt;= 100000; i++)<br />
            {<br />
                values.Add(false);<br />
            }<br />
            return values;<br />
        }<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
