Skip to content

Instantly share code, notes, and snippets.

@dkalamari
Last active December 17, 2015 18:49
Show Gist options
  • Save dkalamari/5655740 to your computer and use it in GitHub Desktop.
Save dkalamari/5655740 to your computer and use it in GitHub Desktop.
Enumerator yield
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