Collection was modified; enumeration operation may not execute in ArrayList [duplicate]

You are removing the item during a foreach, yes? Simply, you can’t. There are a few common options here: use List<T> and RemoveAll with a predicate iterate backwards by index, removing matching items for(int i = list.Count – 1; i >= 0; i–) { if({some test}) list.RemoveAt(i); } use foreach, and put matching items into … Read more

The foreach identifier and closures

Edit: this all changes in C# 5, with a change to where the variable is defined (in the eyes of the compiler). From C# 5 onwards, they are the same. Before C#5 The second is safe; the first isn’t. With foreach, the variable is declared outside the loop – i.e. Foo f; while(iterator.MoveNext()) { f … Read more

Distinction between iterator and enumerator

Iterating means repeating some steps, while enumerating means going through all values in a collection of values. So enumerating usually requires some form of iteration. In that way, enumerating is a special case of iterating where the step is getting a value from a collection. Note the “usually” – enumerating may also be performed recursively, … Read more

What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?

The stop argument to the Block allows you to stop the enumeration prematurely. It’s the equivalent of break from a normal for loop. You can ignore it if you want to go through every object in the array. for( id obj in arr ){ if( [obj isContagious] ){ break; // Stop enumerating } if( ![obj … Read more

Intelligent way of removing items from a List while enumerating in C#

The best solution is usually to use the RemoveAll() method: myList.RemoveAll(x => x.SomeProp == “SomeValue”); Or, if you need certain elements removed: MyListType[] elems = new[] { elem1, elem2 }; myList.RemoveAll(x => elems.Contains(x)); This assume that your loop is solely intended for removal purposes, of course. If you do need to additional processing, then the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)