Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to use yield to return multiple elements from a function one at a time in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

public class Program
{
    public static IEnumerable<int> Get_Fibonacci_Sequence(int total) {
        int returnValue = 1, prev = 0;

        for (int i = 0; i < total; i++) {
            yield return returnValue;

            int temp = returnValue;
            returnValue = prev + returnValue;
            prev = temp;
        }
    }
    public static void Main()
    {
        foreach (int i in Get_Fibonacci_Sequence(11)) {
            Console.Write("{0} ", i);
        }
    }
}




/*
run:

1 1 2 3 5 8 13 21 34 55 89 

*/

 



answered Aug 3, 2022 by avibootz

Related questions

1 answer 64 views
1 answer 146 views
3 answers 112 views
112 views asked Nov 12, 2024 by avibootz
1 answer 113 views
1 answer 183 views
...