Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: C# generators

Login or purchase this course to watch this video and the rest of the course contents.
0:00 This next section, I want to cover something that I think is probably underused in C# and in Python and it's such a cool idea, generators.
0:11 This is the yield return keyword and things like that in C#. So let's look at a real simple example here Fibonacci numbers. So we have two versions.
0:21 We have the naive version of the Fibonacci sequence that you might write and if you don't remember it's 1 and 1 and then you add
0:29 the two numbers together and you get 2 and you add those two to get 2 and 5 and then 8 and then 13 and then 21 and so on.
0:38 Right, so that's the sequence that we're working with here. Here's a simple standard function. Return something IEnumerable of integers
0:46 and you say I would like, I don't know five Fibonacci numbers or six or 1000 or 100,000 and what it does is it creates a list
0:55 it does the algorithm, puts the items in the list as it computes them and when it's done it returns them, great right? This is a standard function
1:03 it returns a basically a list that we can iterate. However, this is an approximation of the Fibonacci numbers in fact, right?
1:12 The Fibonacci sequence is infinite. How do you know how many to ask for? Things like that, it can be tricky. So here's a better one that has no limit.
1:21 See while true and it returns IEnumerable<int> and at each step it uses the yield return keyword to return a single integer.
1:31 So this is how yield return works in C#. We yield return an item of a collection. We say we have a collection of integers
1:38 and here is one and here is one and here's one and it will just keep generating these until we stop asking for them.
1:46 The trick to make this work is we're going to to through them in this foreach loop and get an item out and then if the item is big enough
1:53 then we're going to stop asking. So let's go and run this. Look at it go, runs and runs and runs hits an item, hits an item.
2:02 If we put a breakpoint here this is when it gets pretty interesting. So here we are in our foreach statement
2:07 and if we step in it's going to do what you expect. It goes into the function and great so we're stepping along here. I'll go into while true loop
2:16 it's kind of crazy that that returns out of there but okay that's how these work. So it's going to generate the first item which is 1
2:22 and it's going to return it and go back to the loop, okay? There's one and is equal to 1. We're writing it out and if we keep stepping
2:33 we step back into our Fibonacci sequence but we jump to this line after yield return. Notice even though current started out at 0 it is now 1.
2:44 And this is not because I ran to this place in the function, no. It actually resumes. These are like restartable functions, these generators.
2:53 If we step through here, we're going to do again. Get this, now we're back here and now n is probably 1 again.
3:02 It's going to go 1, 1 and then 2 and so on. So it just keeps pulling them one at a time as we iterate over them. And the result that we get is
3:13 it generates all those until we stop asking for it where we said if it's over a 1000 stop. All right, so this is generators
3:19 and the yield return keyword in C#. Super powerful, it's the foundation of things like the lazy evaluation of things like LINQ for objects and so on.
3:30 I don't think that many people use it but it's really powerful when you want to start work with a collection or generate a collection
3:37 or something like but you're not sure how much you want to take. You want to just pull them one at a time. Really, really nice.
3:43 Yield return keyword, C#, love it.

Talk Python's Mastodon Michael Kennedy's Mastodon