This question is asked many a time in this forum. I know solution for the problem. But I am curious to know why “Enumeration operation can not execute when a collection is modified”
List<string> list = new List<string>();
list.Add("a");
list.Add("b");
int[] array = new int[6] { 1, 2, 3, 4, 5, 5 };
HashSet<int> hashSet = new HashSet<int>();
int i = 0;
foreach (string s in list)
{
list[i] = "test";
i++;
}
But when I change list to list.toarray It works.
Microsoft specifies that any object which implements iEnumerable must void any existing enumerations when the object is modified. The general reason for this requirement is that, for many types of collections, it is difficult to ensure that an enumerator will behave sensibly when a collection is modified. For example, suppose a List holds five values (A,B,C,D,E) and an enumerator works by setting n to the number of elements, and then outputting element(0), element(1), etc. up to element(n-1). If, while the enumerator is enumerating element(2) [C], an element BB is inserted after element(1) [i.e. B], the enumerator might then proceed to output element (3) [which is C, again], then element 4 [D], and then decide it’s done since it output all five elements. That would be a bad situation (one element showed up twice, and one went missing).
It is certainly reasonable that an enumerator should be invalidated if a collection is modified in a way that would prevent the enumerator from yielding sensible results. To my mind, however, enumerators that are capable of meeting the following contract should do so, even if a collection is modified, rather than throwing exceptions:
The VB6-style Collection seems to comply with the above semantics, but none of the other standard collections do. Too bad–such requirements could be met reasonably well by some data structures, but would be sufficient to avoid the duplication of list data which is frequently necessary under the current rules.