..
... Current result=1
Calling MoveNext()...
After yield
About to yield 2
... MoveNext result=True
Fetching Current...
... Current result=2
Calling MoveNext()...
After yield
Yielding final value
... MoveNext result=True
Fetching Current...
... Current result=-1
Calling MoveNext()...
End of GetEnumerator()
... MoveNext result=False
There are various important things to note from this output:
169 C# 2: simple iterators with yield statements
?– None of the code we wrote in GetEnumerator is called until the first call to
MoveNext.
?– Calling MoveNext is the place all the work gets done; fetching Current doesn??™t
run any of our code.
?– The code stops executing at yield return and picks up again just afterwards at
the next call to MoveNext.
?– We can have multiple yield return statements in different places in the method.
?– The code doesn??™t end at the last yield return??”instead, the call to MoveNext
that causes us to reach the end of the method is the one that returns false.
There are two things we haven??™t seen yet??”an alternative way of halting the iteration,
and how finally blocks work in this somewhat odd form of execution. Let??™s take a
look at them now.
6.2.3 Advanced iterator execution flow
In normal methods, the return statement has two effects: First, it supplies the value
the caller sees as the return value.
Pages:
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340