how
Inside, most base set classes support version number. Whenever you add, delete, reorder, etc. Collection, this version number is incremented.
When you start the listing, a snapshot of the version number is taken. Each time around the loop, this version number is compared with the collection, and if they are different, this exception is thrown.
Why
While it would be possible to implement IList so that it can correctly handle changes to collections made in the foreach loop (given that the enumerator keeps track of changes to the collection), the task is much more difficult to handle the changes made to the collection by other threads during the enumeration . Thus, this exception exists to help identify vulnerabilities in your code and provide an early warning of any potential volatility caused by manipulations with other threads.
Drew Noakes Dec 27 2018-10-12T00: 00Z
source share