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