Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello,
I am running into the following error:

Collection was modified; enumeration operation may not execute.

I have a collection that gets modified

Here is the code;

C#
foreach (var m in newSpectrum.PeakCentroids)
                {
                    newSpectrum.PeakCentroids.Remove(m);
                                 }



How do I get around this error?
Posted
Comments
RaisKazi 24-Jul-11 2:13am    
Error is obvious. You are Removing items from the collection, which is traversing through "foreach" loop.
Sergey Alexandrovich Kryukov 24-Jul-11 2:26am    
Correct, I would vote 5.
--SA
Sergey Alexandrovich Kryukov 24-Jul-11 2:37am    
Actually, you explained what was wrong, I added a solution showing how to work around this problem, please see.
--SA
manjudk44 11-Jul-12 6:04am    
Collection was modified; enumeration operation may not execute.same error coming while using above code...help me to get the solution...
I am added gridview...want to bind some subject on each date of a month..
manjudk44 11-Jul-12 6:05am    
protected void Calender1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.DayNumberText == "1" | e.Day.DayNumberText == "2" | e.Day.DayNumberText == "3" | e.Day.DayNumberText == "4" | e.Day.DayNumberText == "5" | e.Day.DayNumberText == "6" | e.Day.DayNumberText == "7" | e.Day.DayNumberText == "8" | e.Day.DayNumberText == "9" | e.Day.DayNumberText == "10" | e.Day.DayNumberText == "11" | e.Day.DayNumberText == "12" | e.Day.DayNumberText == "13" | e.Day.DayNumberText == "14" | e.Day.DayNumberText == "15" | e.Day.DayNumberText == "16" | e.Day.DayNumberText == "17" | e.Day.DayNumberText == "18" | e.Day.DayNumberText == "19" | e.Day.DayNumberText == "20" | e.Day.DayNumberText == "21" | e.Day.DayNumberText == "22" | e.Day.DayNumberText == "23" | e.Day.DayNumberText == "24" | e.Day.DayNumberText == "25" | e.Day.DayNumberText == "26" | e.Day.DayNumberText == "27" | e.Day.DayNumberText == "28" | e.Day.DayNumberText == "29" | e.Day.DayNumberText == "30" | e.Day.DayNumberText == "31")
{
if (e.Day.Date.DayOfWeek != DayOfWeek.Sunday)
{
if (!(e.Day.IsOtherMonth))
{
dt = new DataTable();
dt = obj_calender.load_basedonsubject();
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.HeaderRow.Visible = false;
e.Cell.Font.Italic = true;
e.Cell.Font.Size = FontUnit.Small;
e.Cell.Controls.Add(GridView1);
e.Cell.Width = 50;
e.Cell.Height = 50;
e.Day.IsSelectable = false;
}
}
}
}

Most likely, there is a method Clear, so write newSpectrum.PeakCentroids.Clear().

What if you need to remove just part of elements, on some condition? A most universal solution would be:

C#
using CentroidsList = System.Collection.Generic.List<...>; //put the type of the element of newSpectrum.PeakCentroids

//...

CentroidsList list = new CentroidsList();
foreach (var element in newSpectrum.PeakCentroids)
   if (SomeCondition(element))
       list.Add(element);
foreach (var element in list)
   newSpectrum.PeakCentroids.Remove(element);


It depends what collection is that. In many cases you can write more effective code using for with index, not foreach. Traverse the collection in reverse (from Count - 1 to 0) and delete required elements. In this case deletion will not disrupt the loop.

—SA
 
Share this answer
 
Comments
Abhinav S 24-Jul-11 2:42am    
My 5. Your answer was perhaps a little more detailed while mine was similar but concise. :)
Sergey Alexandrovich Kryukov 24-Jul-11 3:48am    
Thank you, Abhinav.
--SA
RaisKazi 24-Jul-11 3:32am    
Should Work. My 5.
Sergey Alexandrovich Kryukov 24-Jul-11 3:48am    
Thank you.
--SA
If the Collection is fairly small and performance is not the most important need, you can do it this way:

C#
foreach (var m in newSpectrum.PeakCentroids.ToList())
{
    newSpectrum.PeakCentroids.Remove(m);
}
 
Share this answer
 
You cannot modify(remove, add items) the collection while looping through it at the same time.
Its as simple as that.

What you can do is loop through the collection, find the item you want to remove, and then remove the item for the collection.
For removing all items, use clear on the collection.
 
Share this answer
 
v3
Comments
RaisKazi 24-Jul-11 3:33am    
To the Point. 5.
Abhinav S 24-Jul-11 3:36am    
Thanks Rais.
let see if this can help:
C#
//get key collection from dictionary into a list to loop through
List<int> keys = new List<int>(Dictionary.Keys);

// iterating key collection using simple for-each loop
foreach (int key in keys)
{
    // Now we can perform any modification with values of dictionary.
    Dictionary[key] = Dictionary[key] - 1;

}


Resolved Collection Error
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900