集合已修改;枚举操作可能不执行 [英] Collection was modified; enumeration operation may not execute

查看:126
本文介绍了集合已修改;枚举操作可能不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是问了很多时间在这个论坛。我知道这个问题的解决方案。但我很想知道为什么枚举操作不能执行时,集合被修改

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++;
        }

但是,当我改变列表 list.toarray 它的工作原理。

推荐答案

微软指定其实现IEnumerable必须并使任何现有的枚举当对象被修改的对象。一般原因要求是,对于许多类型的集合,它是很难保证的枚举将适宜的方式,当一个集合被修改。例如,假设一个列表包含五个值(A,B,C,D,E)和一个枚举器的工作原理是将N设置为元素的数量,然后输出单元(0),元件(1),等多达元件(N-1)。如果当枚举器枚举元素(2)[C],元素BB后,元件(1)[即插入B],普查员可能会再进行输出元件(3)为C,再次],然后元素4研究[D],然后再决定它的,因为它输出的所有五行完成。这将是一个糟糕的情况(一个元素出现了两次,一人失踪)。

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).

这当然是合理的,如果一个集合中,将prevent从产生有意义的结果的枚举的方式修改的枚举应该无效。在我看来,不过,统计员,它们能够满足下列合同应该这样做,即使一个集合被修改,而不是抛出异常:

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:

  1. 在其存在于整个枚举的时间必须精确,一旦返回的任何项目。
  2. 其中存在枚举的持续时间的一部分的任何项目必须返回恰好一次或根本没有,但没有关于此种产品(如果有的话)不返回任何要求。
  3. 在该项目恢复必须是枚举一个有效的序列中的序列(如排序列表必须按排序顺序返回的项目;如果项目被添加到这将precede一个已经输出的项目列表中,该项目不得枚举)。
  4. 为了(1)和(2),该被除去应视为不同从一个被添加,即使密钥相同的项目;如果项的键被改变时,变化前的项目被认为是从后的项目是不同的。
  5. 通过复位反复枚举不能保证返回相同的项目。
  1. Any item which exists throughout the duration of enumeration must be returned exactly once.
  2. 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.
  3. 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).
  4. 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.
  5. Repeated enumerations via Reset are not guaranteed to return the same items.

的VB6风格类别似乎符合上述语义,但是没有其他标准集合做。太糟糕了 - 这样的要求可以得到满足合理地通过一些数据结构,但足以避免列表数据的重复是经常需要在现行规则

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.

这篇关于集合已修改;枚举操作可能不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆