Last active
December 17, 2015 18:49
-
-
Save dkalamari/5655740 to your computer and use it in GitHub Desktop.
Enumerator yield
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static IEnumerable<int> Fibs (int fibCount) | |
{ | |
for (int i = 0, prevFib = 1, curFib = 1; i < fibCount; i++) | |
{ | |
yield return prevFib; | |
int newFib = prevFib+curFib; | |
prevFib = curFib; | |
curFib = newFib; | |
} | |
} | |
/* | |
On each yield statement, control is re-turned to the caller, but the | |
callee’s state is maintained so that the method can | |
continue executing as soon as the caller enumerates the next element. | |
A return statement is illegal in an iterator block—you must use | |
a yield breakinstead. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment