0

I am getting this exception:

Collection was modified; enumeration operation may not execute.

Directly after this code:

     else if (count == 0)
                    {
                        partCategory.Add(words[count].ToString());
                        foreach (string categoryS in partCategory)
                        {
                            if (categoryS != words[count].ToString())
                            {
                                partCategory.Add(words[count].ToString());
                            }
                        }
                    }

I am taking a guess that it may be the Add method that was used prior to it, but I'm not sure.

Call Stack Code:

**********.exe!**********m.FileController.readSortText(string fileName = "**********extracted\\PC120701.txt", System.Windows.Forms.ProgressBar newProgress = {Value = 0 Min = 0 Max = 100}) Line 78 + 0x207 bytes   C#

**********.exe!**********.mainForm.unzipFile(string fileName = "**********") Line 133 + 0x42 bytes  C#
**********.exe!**********.mainForm.decompress_Click(object sender = {Text = "Unzip"}, System.EventArgs e = {X = 49 Y = 7 Button = Left}) Line 163 + 0x17 bytes  C#

[External Code] 
**********.exe!**********.Program.Main() Line 18 + 0x28 bytes   C#

[External Code] 

Thanks for your future help!

Vasa Serafin
  • 306
  • 5
  • 18

4 Answers4

2

According to the foreach documentation: Accents mine.

The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface. The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
1

Yes that's because you are modifying the collection you are enumerating.

Adil
  • 3,248
  • 1
  • 22
  • 27
  • I found a similar question as well. Check this out http://stackoverflow.com/questions/604831/collection-was-modified-enumeration-operation-may-not-execute and http://social.msdn.microsoft.com/forums/en/netfxbcl/thread/7ce02724-2813-4f7d-8f3c-b1e3c1fd3019/ – Adil Jul 01 '12 at 04:26
1

you can't change a list while reading the list by foreach command you can use for command or take another list and keep values in the new one and then in another for loop put new values to old one

DeveloperX
  • 4,633
  • 17
  • 22
1

MSDN is your friend. Have a look here. The foreach statement is using the IEnumerator interface behind the scenes.

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException. If the collection is modified between MoveNext and Current, Current returns the element that it is set to, even if the enumerator is already invalidated.

Aesthete
  • 18,622
  • 6
  • 36
  • 45