I have collection:
public class myList : List<myClass> { }
And do:
foreach (myClass obj in myList) {
//...
if (some_condition) {
myList.Add(newObj); }
//...
}
So before 2 iteration i get error like: collection was changed
. Obviously problem is in myList.Add(newObj);
.
How can i organize this loop to avoid errors? Keep in mind: i can add some elements to list but never remove.
I can just use for
loop with iterator. But in this case my code will look a bit worse. Maybe here is some better solution?