Generating the Fibonacci sequence by using the yield keyword, non-recursively

roger's picture

class FibonacciSequence : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
        yield return (long)0;
        yield return (long)1;

        long prev = 0;
        long curr = 1;

        for (; ; )
        {
            long next = prev + curr;
            if (next < 0)   // It overflowed. Stop.
                yield break;

            yield return next;
            prev = curr;
            curr = next;
        }
    }
}

Call it like this:

FibonacciSequence f = new FibonacciSequence(); 

foreach (long n in f)
    Console.WriteLine(n);