Collection was modified; enumeration operation may not execute [duplicate]
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.
In general .Net collections do not support being enumerated and modified at the same time. The line list[i] = "test"
modifies the collection list
while you are in the middel of enumerating it and hence and exception is generated. True this is a trivial modification and doesn't affect the structure of the list but List<T>
treats even in place modifications as destructive.
The ToArray
version works because now you have 2 collections
list
- The created array
You are actually then enumerating the array and hence modifying the original list
is just fine.
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:
- Any item which exists throughout the duration of enumeration must be returned exactly once.
- Any item which exists for part of the duration of enumeration must be returned exactly once or not at all, but there is no requirement regarding which such items (if any) are returned.
- The sequence in which the items are returned must be a valid sequence for the enumerator (e.g. a SortedList must return items in sorted sequence; if an item gets added to the list which would precede an already-output item, that item must not be enumerated).
- For purposes of (1) and (2), an item which is removed shall be considered distinct from one which is added, even if the keys are identical; if an item's key is changed, the item before the change is considered distinct from the item after.
- Repeated enumerations via Reset are not guaranteed to return the same items.
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.
When you do list.ToArray()
in your foreach, you're making an entire copy of the list contents, and enumerating over the copy. You're no longer making a change to the list while enumerating over the list.
Doing this:
foreach (string s in list.ToArray())
{
list[i] = "test";
i++;
}
is effectively the same as doing this:
string[] newArray = list.ToArray();
foreach (string s in newArray)
{
list[i] = "test";
i++;
}
Note that, in the second case, you're not enumerating over the collection you are modifying, but rather an entirely new collection.
精彩评论