Yield5:14 with Jeremy McLain
Yeild can also be used in methods that return either IEnumerator or IEnumerable.
There's a bit more you want to know about the yield keyword. 0:00 We can use the yield keyword in any method that returns either I enumerator or 0:03 I enumerable. 0:09 We just saw an example of using yield in a method that returns I enumerator. 0:10 When implementing the git numerator method, 0:14 a more common use of it is in methods the return I enumerable. 0:17 To demonstrate how this works, 0:21 let's rewrite a method that is already provided by link. 0:23 It's a great example of how to use yield. 0:27 We will write this method in a new class. 0:30 I'll just call it utils. 0:33 I'll start by getting rid of these using directives we don't need up here. 0:45 And we'll make this a public static class. 0:50 We'll write a method called Take. 0:55 Take returns the first n items in the collection passed in. 0:58 And it returns them as a collection of type IEnumerable of T. 1:02 So I'll say public static IEnumerable of T, 1:06 Of course I need to add the using directive for that. 1:10 And the method's called Take. 1:18 And here's where we specify the generic parameter. 1:22 I'll say IEnumerable<t>, 1:25 and call this parameter source. 1:32 And the second parameter we'll just call N. 1:35 So take we'll take the first N items from the source collection and 1:41 return them as an I numerable of T. 1:46 Notice that take is a generic method, 1:49 you can have generic methods outside of a generic class. 1:51 In fact, in my experience generic methods are more common than generic classes. 1:55 The type parameter is specified right here after the name of the method. 2:01 Now T can be used anywhere in the method and 2:05 this method we'll find the first n items by looping through the source collection. 2:09 We'll need a counter to keep track of how many items we've returned. 2:14 So I'll say int i = 0 and we'll have a for 2:18 each loop that loops through each of the items and source. 2:23 So we'll say, var item in source and 2:26 we'll yield return each item. 2:31 Once we've yielded an item's, 2:39 we can signal the end of the collection with the yield break statement. 2:41 So I'll say if I don't need to increment i here in order to keep track of how many 2:45 items we've seen so far and I'll do it with a pre-increment. 2:50 So I'll say plus plus i. 2:54 And then I'll check to see if that's N. 2:58 And if it is, we'll say yield break. 3:02 The take method is returning an IEnumerable of T, 3:07 instead of an IEnumerator of T. 3:11 What's happening, is by using yield return, 3:13 We are telling C sharp to construct an empty I Enumerable collection. 3:17 The body of the take method is called, when get a numerator is called, 3:21 by a foreach loop that's looping through the I numerable of T. 3:25 that's returned by the take method. 3:30 Yield break here tells the move next method of the numerator to return false. 3:33 Thus signaling the end of the enumeration. 3:39 Over in main, et's see how to use this method. 3:42 So here I'll make a variable of type I Enumerable of 3:49 T and I'll call it first three. 3:52 And a set equal to Utils.Take. 3:59 And it will just take list1 from above and get the first 3 items. 4:05 Instead of T here. 4:16 This should be int. 4:17 Now, we can loop through the first three collection. 4:21 So we'll say foreach var item in firstThree. 4:24 The neat thing about numerators is that even after 4:28 calling the take method on this line, firstThree still doesn't contain anything. 4:34 not until the foreach loop is run but the body of the take method is executed. 4:40 This is true of all methods that use yield. 4:45 It's called lazy evaluation. 4:49 The items in the collection aren't retrieved until they're needed. 4:51 Yield is a really handy feature of C-sharp. 4:55 It provides data just in time, right when it's needed. 4:59 It's actually quite versatile. 5:03 I've used it to write methods that read from files and network buffers. 5:05 I've included some links in the teacher's notes if you'd like to learn 5:09 more about yield. 5:12
You need to sign up for Treehouse in order to download course files.Sign up