debugging - C# - foreach showing strange behavior / for working with no problem -


today coded function uses 2 nested foreach loops. after seeing, did not work expected, debugged it. dont see error, , dont think simple error can cause behavior have noticed.

the part looks this:

foreach(myclass citem in checkedlistboxitemlist.items) {    foreach(myclass cactiveitem in activeitemlist)    {       if (cactiveitem.id == citem.id) /*...check checkbox item...*/;    } } 

lets say, checkedlistboxitemlist.items holds 4 items of type myclass, , activeitemlist list< myclass > 2 items.

the debugger jumps outer foreach, reaches inner foreach, executes if 2 times (once per cactiveitem) , reaches end of outer foreach.now, debugger jumps head of outer foreach should. instead of starting second round of outer foreach, debugger jumps myclass.tostring() method. can step through method 4 times (number of items in checkedlistboxitemlist.items) , ... nothing. visual studio shows me windows form, , foreach not continued.

when changing code to

int listcount = checkedlistboxitemlist.items.count; for(int i=0; i<listcount; i++) {    myclass citem = checkedlistboxitemlist.items[i] myclass;    foreach(myclass cactiveitem in activeitemlist)    {       if (cactiveitem.id == citem.id) /*...check checkbox item...*/;    } } 

everything works fine , supposed. showed problem collegue, didnt understand, happened. dont understand why debugger jumps myclass.tostring() method. used f10 step through, no need leave function. , even, if there reason, why isnt foreach loop continued?

im using visual studio 2010, if of matter.

please tell me happened. thanks.

when iterating collection (using foreach), collection iterating not allowed change; when check checkbox matching item, collection of outer loop (checkedlistboxitemlist.items) changes , consequent error thrown swallowed somewhere. more or less explains why go tostring method , don't continue loop.

when use forstatement iterate, don't have restriction there no reference collection @ moment start iterating.

hope explains.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -