/IEnumerable

It's a program, which is calculating Fibonacci numbers, using yield return(.net framework 4.6,C# 6.0).

Primary LanguageC#

IEnumerable

In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones:

fib1

using System;
using System.Collections.Generic;
using System.Linq;


namespace IEnumerable_interf
{
    
    class Program
    {
          static void Main(string[] args)
        {
            foreach (var item in Fib().Skip(0).Take(20))
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
        static  IEnumerable<int> Fib()
        {
            int current = 0;
            int next = 1;
            while (true)
            {
                yield return current;
                int temp = current;
                current = next;
                next = temp + current;
            }
        }
    }
}

Also we can skip some elements.

fib

We can't use the ref and out keywords for the Iterator methods, which include a yield return or yield break statement.