Collection was modified; enumeration operation may not execute - why?

前端 未结 7 1775
南方客
南方客 2020-11-30 11:38

I\'m enumerating over a collection that implements IList, and during the enumeration I am modifying the collection. I get the error, \"Collection was modified; enumeration

7条回答
  •  温柔的废话
    2020-11-30 11:53

    This is the specified behavior:

    Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.

    This limitation makes enumerators simpler to implement.
    Note that some collections will (incorrectly) allow you to enumerate while modifying the collection.

提交回复
热议问题