
Yield return in C# - Kennethtruyers
https://www.kenneth-truyers.net/2016/05/12/yield-return-in-c/
======
shaftway
I liked the article. Maybe a bit about how the LinQ methods are (or could be)
implemented under the hood to show how easy this can actually be?

I've always like the good old fibonacci sequence for this too, because it
shows off some simple logic that we all know, but it touches on some non-
intuitive aspects (like yields being anywhere, and infinitely long
IEnumerables).

    
    
        public IEnumerable<int> Fib() {
          yield return 1;
          yield return 1;
    
          int a = 1, b = 1;
          while (true) {
            b = a + b;
            a = b - a;
            yield return b;
          }
        }
    

Then the 500th fibonachi number is:

    
    
        Fib().Skip(499).Next()

------
Kennethtruyers
Any questions are welcome!

~~~
krylon
Honest question: Is this just coroutines or am I missing something?

(Either way, I did not know C# had this feature, it looks very nice! Also, the
article is very well written, thanks a lot!)

~~~
Kennethtruyers
Yes, yield return (and iterators in general) are the coroutine implementation
in c#.

Here's some more info (although outdated):
[https://en.wikipedia.org/wiki/Coroutine#Implementations_for_...](https://en.wikipedia.org/wiki/Coroutine#Implementations_for_C.23)

Thanks, glad you enjoyed it!

------
Bertengele
Good article! A complex thing explained very cleanly.

