Practice mode C#

When does the body of a C# iterator method that uses yield return start running?

Correct answer D. When the caller first calls MoveNext, usually through foreach

Explanation

Calling an iterator method just creates the enumerator object; none of its code runs yet, not even argument checks. Execution begins on the first MoveNext call, such as at the start of a foreach. That is why argument validation in iterators is often moved to a wrapper method or local function, so errors appear at call time.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.