Catching behavior in a foreach loop C#
-
29-10-2019 - |
Question
Simple question, I believe --> It is my understanding that an imaginary IEnumerator object is being used when I use a foreach loop over an IEnumerable object. My question is as follows:
How can I "catch" illegal behavior in foreach loops dealing with my objects? I specifically want to see if my original object from which the IEnumerable was created has been modified. If the original has been modified, I want to throw an exception.
My current approach has been to use a version number. This works great if I create an explicit iterator and use a MoveNext() or somesuch, but foreach loops seem to trick my version-based approach.
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow