1 00:00:00,470 --> 00:00:03,640 There's a bit more you want to know about the yield keyword. 2 00:00:03,640 --> 00:00:09,160 We can use the yield keyword in any method that returns either I enumerator or 3 00:00:09,160 --> 00:00:10,420 I enumerable. 4 00:00:10,420 --> 00:00:14,930 We just saw an example of using yield in a method that returns I enumerator. 5 00:00:14,930 --> 00:00:17,460 When implementing the git numerator method, 6 00:00:17,460 --> 00:00:21,480 a more common use of it is in methods the return I enumerable. 7 00:00:21,480 --> 00:00:23,380 To demonstrate how this works, 8 00:00:23,380 --> 00:00:27,600 let's rewrite a method that is already provided by link. 9 00:00:27,600 --> 00:00:30,740 It's a great example of how to use yield. 10 00:00:30,740 --> 00:00:33,410 We will write this method in a new class. 11 00:00:33,410 --> 00:00:34,647 I'll just call it utils. 12 00:00:45,305 --> 00:00:48,130 I'll start by getting rid of these using directives we don't need up here. 13 00:00:50,260 --> 00:00:52,550 And we'll make this a public static class. 14 00:00:55,460 --> 00:00:57,170 We'll write a method called Take. 15 00:00:58,340 --> 00:01:01,880 Take returns the first n items in the collection passed in. 16 00:01:02,900 --> 00:01:06,210 And it returns them as a collection of type IEnumerable of T. 17 00:01:06,210 --> 00:01:10,998 So I'll say public static IEnumerable of T, 18 00:01:10,998 --> 00:01:16,544 Of course I need to add the using directive for that. 19 00:01:18,531 --> 00:01:19,932 And the method's called Take. 20 00:01:22,624 --> 00:01:25,458 And here's where we specify the generic parameter. 21 00:01:25,458 --> 00:01:32,130 I'll say IEnumerable, 22 00:01:32,130 --> 00:01:35,960 and call this parameter source. 23 00:01:35,960 --> 00:01:38,590 And the second parameter we'll just call N. 24 00:01:41,200 --> 00:01:46,050 So take we'll take the first N items from the source collection and 25 00:01:46,050 --> 00:01:48,190 return them as an I numerable of T. 26 00:01:49,310 --> 00:01:51,970 Notice that take is a generic method, 27 00:01:51,970 --> 00:01:55,900 you can have generic methods outside of a generic class. 28 00:01:55,900 --> 00:02:01,540 In fact, in my experience generic methods are more common than generic classes. 29 00:02:01,540 --> 00:02:05,970 The type parameter is specified right here after the name of the method. 30 00:02:05,970 --> 00:02:09,570 Now T can be used anywhere in the method and 31 00:02:09,570 --> 00:02:14,920 this method we'll find the first n items by looping through the source collection. 32 00:02:14,920 --> 00:02:18,850 We'll need a counter to keep track of how many items we've returned. 33 00:02:18,850 --> 00:02:23,510 So I'll say int i = 0 and we'll have a for 34 00:02:23,510 --> 00:02:26,450 each loop that loops through each of the items and source. 35 00:02:26,450 --> 00:02:31,920 So we'll say, var item in source and 36 00:02:31,920 --> 00:02:36,688 we'll yield return each item. 37 00:02:39,503 --> 00:02:41,323 Once we've yielded an item's, 38 00:02:41,323 --> 00:02:45,960 we can signal the end of the collection with the yield break statement. 39 00:02:45,960 --> 00:02:50,910 So I'll say if I don't need to increment i here in order to keep track of how many 40 00:02:50,910 --> 00:02:54,300 items we've seen so far and I'll do it with a pre-increment. 41 00:02:54,300 --> 00:02:55,845 So I'll say plus plus i. 42 00:02:58,578 --> 00:03:01,140 And then I'll check to see if that's N. 43 00:03:02,530 --> 00:03:05,980 And if it is, we'll say yield break. 44 00:03:07,500 --> 00:03:11,280 The take method is returning an IEnumerable of T, 45 00:03:11,280 --> 00:03:13,770 instead of an IEnumerator of T. 46 00:03:13,770 --> 00:03:17,010 What's happening, is by using yield return, 47 00:03:17,010 --> 00:03:21,660 We are telling C sharp to construct an empty I Enumerable collection. 48 00:03:21,660 --> 00:03:25,927 The body of the take method is called, when get a numerator is called, 49 00:03:25,927 --> 00:03:30,650 by a foreach loop that's looping through the I numerable of T. 50 00:03:30,650 --> 00:03:33,150 that's returned by the take method. 51 00:03:33,150 --> 00:03:39,490 Yield break here tells the move next method of the numerator to return false. 52 00:03:39,490 --> 00:03:42,830 Thus signaling the end of the enumeration. 53 00:03:42,830 --> 00:03:45,890 Over in main, et's see how to use this method. 54 00:03:49,540 --> 00:03:52,720 So here I'll make a variable of type I Enumerable of 55 00:03:52,720 --> 00:03:57,280 T and I'll call it first three. 56 00:03:59,560 --> 00:04:05,820 And a set equal to Utils.Take. 57 00:04:05,820 --> 00:04:14,350 And it will just take list1 from above and get the first 3 items. 58 00:04:16,260 --> 00:04:17,700 Instead of T here. 59 00:04:17,700 --> 00:04:18,520 This should be int. 60 00:04:21,670 --> 00:04:24,710 Now, we can loop through the first three collection. 61 00:04:24,710 --> 00:04:28,880 So we'll say foreach var item in firstThree. 62 00:04:28,880 --> 00:04:34,500 The neat thing about numerators is that even after 63 00:04:34,500 --> 00:04:40,350 calling the take method on this line, firstThree still doesn't contain anything. 64 00:04:40,350 --> 00:04:45,700 not until the foreach loop is run but the body of the take method is executed. 65 00:04:45,700 --> 00:04:49,150 This is true of all methods that use yield. 66 00:04:49,150 --> 00:04:51,910 It's called lazy evaluation. 67 00:04:51,910 --> 00:04:55,930 The items in the collection aren't retrieved until they're needed. 68 00:04:55,930 --> 00:04:59,180 Yield is a really handy feature of C-sharp. 69 00:04:59,180 --> 00:05:03,400 It provides data just in time, right when it's needed. 70 00:05:03,400 --> 00:05:05,640 It's actually quite versatile. 71 00:05:05,640 --> 00:05:09,920 I've used it to write methods that read from files and network buffers. 72 00:05:09,920 --> 00:05:12,990 I've included some links in the teacher's notes if you'd like to learn 73 00:05:12,990 --> 00:05:13,840 more about yield.