Create Fibonacci With Tuples - CSharp Language Basics

CSharp examples for Language Basics:Tuple

Description

Create Fibonacci With Tuples

Demo Code

using System;// w ww .  j  a va 2s  . co  m
using System.Collections.Generic;
using System.Linq;
class FibonacciWithTuples
{
   static void Main()
   {
      foreach (var value in Fibonacci().Take(10))
      {
         Console.WriteLine(value);
      }
   }
   static IEnumerable<int> Fibonacci()
   {
      var pair = (current: 0, next: 1);
      while (true)
      {
         yield return pair.current;
         pair = (pair.next, pair.current + pair.next);
      }
   }
}

Result


Related Tutorials