Create Fibonacci Without Tuples - CSharp Language Basics

CSharp examples for Language Basics:Tuple

Description

Create Fibonacci Without Tuples

Demo Code

using System;//from  ww  w . ja va  2 s  .c  om
using System.Collections.Generic;
using System.Linq;
class FibonacciWithoutTuples
{
   static void Main()
   {
      foreach (var value in Fibonacci().Take(10))
      {
         Console.WriteLine(value);
      }
   }
   static IEnumerable<int> Fibonacci()
   {
      int current = 0;
      int next = 1;
      while (true)
      {
         yield return current;
         int nextNext = current + next;
         current = next;
         next = nextNext;
      }
   }
}

Result


Related Tutorials