1

I know a class can implement both IEnumerable and IEnumerator (its GetEnumerator method will simply "return this" and there won't be any compile-time issue, but I've read that we should avoid this practice as this can cause problems if we called GetEnumerator() a second time before the first enumeration was completed.

I'm still confused about why we should avoid this and how can it cause a problem. It would be great if someone could explain this in detail for greater understanding.

karel
  • 5,489
  • 46
  • 45
  • 50
Gaurav Ahuja
  • 885
  • 1
  • 7
  • 11
  • 1
    but you answered on this - `this can cause problems if we called GetEnumerator() a second time before the first enumeration was completed` – tym32167 Jun 08 '18 at 10:23
  • An Enumerator has to keep a State (where was I). And that doesn't mix with "call GetEnumerator() a second time before the first enumeration was completed" – bommelding Jun 08 '18 at 10:24
  • You can read this: [What is the difference between IEnumerator and IEnumerable](https://stackoverflow.com/questions/619564/what-is-the-difference-between-ienumerator-and-ienumerable) for reference. – kurakura88 Jun 08 '18 at 10:26

0 Answers0