The collection looks like this:
public class myList : List<myClass> { } I need to repeat the cycle until it adds new elements:
do { newObjectAdded = false; foreach (myClass obj in myList) { //... if (some_condition) { myList.Add(newObj); newObjectAdded = true;} //... } } while (newObjectAdded); After an element is added to the sheet, in the foreach (myClass obj in myList) line foreach (myClass obj in myList) I get an error - the collection has been changed, it is impossible to perform an enumeration operation.
You can replace foreach with a loop with an iterator, but this will make accessing objects rather cumbersome. There is also the option of replacing the list with LinkedList, but this is even worse, in this case.
Is there some more elegant way?