user3662528
user3662528

Reputation: 21

'System.Collections.IEnumerator' does not contain a public definition for 'GetEnumerator'

I'm trying to use yield mechanizmi in C#:

public void ScanLoop( OnScanStatus statusCallback, int graphId = -1 ) {
    foreach ( int it in _scanLoop( statusCallback, graphId ) ) {
    }
}

public IEnumerator _scanLoop( OnScanStatus statusCallback, int graphId ) {
    ...
    }

The error i get during the compilation is:

Error 23 foreach statement cannot operate on variables of type 'System.Collections.IEnumerator' because 'System.Collections.IEnumerator' does not contain a public definition for 'GetEnumerator'

can anyone tell my why? it seems strange from what i can see IEnumerable is the interface containing GetEnumerator

Upvotes: 1

Views: 2350

Answers (1)

Ben Aaronson
Ben Aaronson

Reputation: 6975

GetEnumerator returns an IEnumerator, but it's a method on IEnumerable.

foreach requires an IEnumerable, and will get the IEnumerator internally. Most of the time (e.g. using foreach or LINQ) you will never need to handle the IEnumerator yourself, unless for some reason you were trying to implement your own iterable collection type.

Upvotes: 7

Related Questions