When implementing iterator using yield return, is there any difference between returning IEnumerator and IEnumerable?
When implementing iterator using yield return , is there any difference between returning IEnumerator
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
IEnumerableandIEnumeratorare two different things.IEnumerable<T>is a sequence that can be iterated over.IEnumerator<T>is an object that is returned byIEnumerable<T>to iterate once over the sequence.In general, the only place to return
IEnumerator<T>is in theGetEnumerator()method.yield returnbehaves the same way for both types, except that an iterator method that returnsIEnumerable<T>can execute multiple times (each time the sequence is enumerated).For more information on how this works, see Jon Skeet’s article.