News for hackers. Stuff that matters.
Re:Yield return in C# by shaftway on Thursday May 12, 2016 @11:44AM (#11684229)
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()
Re:Yield return in C# by Kennethtruyers on Thursday May 12, 2016 @08:30AM (#11682843)
Any questions are welcome!
Re:Yield return in C# by djengineerllc on Thursday May 12, 2016 @08:33AM (#11682861)
I like this article. It was very well laid out. I've already added a new card to my Trello board for taking a look at implementing "yield" in our software application.
Re:Yield return in C# by Kennethtruyers on Thursday May 12, 2016 @08:39AM (#11682894)
Thanks! Great to hear it's of use.
Re:Yield return in C# by krylon on Thursday May 12, 2016 @09:22AM (#11683170)
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!)

Re:Yield return in C# by Kennethtruyers on Thursday May 12, 2016 @09:26AM (#11683183)
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_...

Thanks, glad you enjoyed it!

Re:Yield return in C# by JeanSebTr on Thursday May 12, 2016 @11:15AM (#11683968)
There's no coroutines involved.

It use a simple state machine. The async keyword is implemented with the same principle.

Re:Yield return in C# by Bertengele on Thursday May 12, 2016 @08:44AM (#11682921)
Good article! A complex thing explained very cleanly.