1 00:00:01,040 --> 00:00:06,320 ‫And this video, we're going to look at some very important interfaces that you should understand once 2 00:00:06,320 --> 00:00:09,930 ‫you want to become a more advanced shop developer. 3 00:00:09,950 --> 00:00:16,130 ‫And that is the AI enumerable interface as well as the AI enumerator interface. 4 00:00:16,610 --> 00:00:21,740 ‫So you should understand interfaces before watching this video, because otherwise some of the concepts 5 00:00:21,770 --> 00:00:23,540 ‫won't make sense to you potentially. 6 00:00:23,540 --> 00:00:28,520 ‫So I really recommend to check out that the interface video that we have to offer as well. 7 00:00:28,940 --> 00:00:29,490 ‫Okay. 8 00:00:29,510 --> 00:00:36,440 ‫So the AI enumerable interface is the base interface for many collections in C sharp and its job is 9 00:00:36,440 --> 00:00:39,950 ‫to provide a way to iterate through a collection. 10 00:00:40,460 --> 00:00:47,510 ‫So this is why we can use for each loops to go through a list or a dictionary because they are inheriting 11 00:00:47,510 --> 00:00:50,090 ‫from the enumerable interface. 12 00:00:50,090 --> 00:00:55,850 ‫So the enumerable interface in simple English, when the collection class implements the AI enumerable 13 00:00:55,850 --> 00:01:02,630 ‫interface, it becomes countable and we can count each element in it individually. 14 00:01:02,690 --> 00:01:07,310 ‫So there are two different versions of the AI enumerable interface. 15 00:01:07,520 --> 00:01:14,780 ‫One is that works for generic collections where you have this AI enumerable opening bracket, closing 16 00:01:14,780 --> 00:01:20,240 ‫bracket, and then the AI enumerable interface for non generic collections. 17 00:01:20,840 --> 00:01:24,530 ‫So you should understand what generic and non generic collections are as well. 18 00:01:24,530 --> 00:01:29,390 ‫They have certain advantages and disadvantages, but I have a video for that as well. 19 00:01:29,390 --> 00:01:33,710 ‫I hope you watched it before watching this video because otherwise it's a little complicated. 20 00:01:34,010 --> 00:01:34,580 ‫Okay. 21 00:01:34,580 --> 00:01:37,760 ‫So these two that we have here, let's look at those. 22 00:01:37,910 --> 00:01:44,510 ‫Since the generic collections were introduced after the non generic collections, it's recommended to 23 00:01:44,510 --> 00:01:46,070 ‫use the generic collection. 24 00:01:46,070 --> 00:01:49,190 ‫So they are basically an improved version, so to speak. 25 00:01:50,180 --> 00:01:56,900 ‫So if you are creating a new project, it's recommended to use the generic version because an I enumerable 26 00:01:56,900 --> 00:02:04,490 ‫for non generic collections needs to apply or perform boxing and unboxing, which basically is the conversion 27 00:02:04,490 --> 00:02:06,470 ‫of types of objects. 28 00:02:06,470 --> 00:02:12,830 ‫And we will later explain how to use the AI enumerable tty interface. 29 00:02:12,830 --> 00:02:19,610 ‫So the generic collections interface, we're not going to go over the non generic collections interface 30 00:02:19,610 --> 00:02:24,950 ‫because it's not recommended due to the boxing and unboxing, which just is less efficient. 31 00:02:25,040 --> 00:02:33,170 ‫So now let's look at the difference between the AI enumerable and the AI enumerator. 32 00:02:33,590 --> 00:02:35,570 ‫You maybe have heard those before. 33 00:02:35,570 --> 00:02:42,080 ‫So the AI enumerable interface contains a single method that you must implement when implementing this 34 00:02:42,080 --> 00:02:45,950 ‫interface, and we're going to do that later on in our particular example here. 35 00:02:45,950 --> 00:02:47,540 ‫So it makes a lot more sense. 36 00:02:47,990 --> 00:02:55,880 ‫So the get enumerate method is the one that you need to implement and it returns an AI enumerator object. 37 00:02:56,270 --> 00:03:03,140 ‫So this method that you need to implement when you want to use a AI enumerable for your class or what 38 00:03:03,140 --> 00:03:10,670 ‫you want to implement it for your class, you need to have this get enumerator method which then returns 39 00:03:10,670 --> 00:03:11,810 ‫an AI enumerator. 40 00:03:11,810 --> 00:03:13,880 ‫So the AI enumerator. 41 00:03:14,740 --> 00:03:22,300 ‫Provides the ability to reiterate through the collection by exposing a current property that points 42 00:03:22,300 --> 00:03:26,350 ‫at the object we are currently at in the collection. 43 00:03:26,980 --> 00:03:34,960 ‫This is a lot more complicated than it will be once we actually run this in our particular example. 44 00:03:35,200 --> 00:03:39,820 ‫So we're going to build an example fully understanding what all of this means. 45 00:03:40,150 --> 00:03:43,270 ‫So this is just to prepare you what you can expect. 46 00:03:43,750 --> 00:03:45,850 ‫Okay, so let's get started. 47 00:03:46,660 --> 00:03:50,970 ‫Finally getting away from all of this theory, let's actually look into practice. 48 00:03:50,980 --> 00:03:54,360 ‫So let's create this little class called dog. 49 00:03:54,370 --> 00:03:57,400 ‫And I'm going to put it in the same file to keep things simple. 50 00:03:57,490 --> 00:04:03,970 ‫So we have this class called Dog with a name and we have this Boolean called is Naughty Dog. 51 00:04:04,000 --> 00:04:08,350 ‫So we will basically check if the dog is naughty or not. 52 00:04:08,350 --> 00:04:11,170 ‫And based on that, we are going to give him treats. 53 00:04:11,440 --> 00:04:17,350 ‫So if he's not a naughty dog, he will get less treats than if he's a good boy or a good dog. 54 00:04:17,860 --> 00:04:21,430 ‫So the constructor here basically just sets those two values. 55 00:04:21,430 --> 00:04:26,530 ‫So when we create a dog object, we are going to give it a name and a boolean, whether the dog was 56 00:04:26,530 --> 00:04:27,400 ‫naughty or not. 57 00:04:27,790 --> 00:04:36,270 ‫And then we have this gift treat method and this will basically just print out the dog said Woof. 58 00:04:36,280 --> 00:04:42,540 ‫So for every single treat that the dog is given, he will say Woof. 59 00:04:43,390 --> 00:04:48,160 ‫So basically, if he's a good dog, you will get two treats and he will move twice. 60 00:04:48,160 --> 00:04:54,910 ‫And if he's a bad dog or a naughty dog, then he will only move once because he will get less treats. 61 00:04:57,670 --> 00:04:57,840 ‫Okay. 62 00:04:58,090 --> 00:05:04,140 ‫So now we have this class dock and we can now go ahead and create dog objects. 63 00:05:04,180 --> 00:05:06,220 ‫So let's have this other class. 64 00:05:06,220 --> 00:05:13,210 ‫Let's assume we have another class that will use dogs inside of a list. 65 00:05:13,510 --> 00:05:15,760 ‫So we have this dog shelter class. 66 00:05:16,120 --> 00:05:20,860 ‫So this dog shelter class will have a list of dogs. 67 00:05:21,070 --> 00:05:27,730 ‫So therefore we need to make sure that we have this using system collections generic because list is 68 00:05:27,730 --> 00:05:29,410 ‫a generic collection. 69 00:05:29,830 --> 00:05:35,110 ‫So here at the top you will see that now you have this using system collections generic. 70 00:05:35,820 --> 00:05:36,160 ‫Okay. 71 00:05:36,190 --> 00:05:42,200 ‫So now that we have this dog shelter and we have this list of dogs in the constructor of this dog shelter, 72 00:05:42,250 --> 00:05:47,820 ‫the only thing that we're going to do is to initialize this dog's list with a bunch of dogs. 73 00:05:47,830 --> 00:05:50,940 ‫So we have the Casper dog or a dog called Casper. 74 00:05:50,950 --> 00:05:58,080 ‫He's not an old dog, but if is a naughty dog, Oreo is not naughty and Pixel is also not naughty. 75 00:05:58,090 --> 00:06:03,340 ‫So don't ask me how we come up with these names, but that's basically how we're going to call those 76 00:06:03,340 --> 00:06:03,620 ‫dogs. 77 00:06:03,640 --> 00:06:09,010 ‫You could, of course, make this whole application a lot more complex, but our idea is really just 78 00:06:09,010 --> 00:06:13,870 ‫to use this list of dogs to iterate through the dog shelter, to go through the dog shelter, basically. 79 00:06:13,870 --> 00:06:19,390 ‫And based on whether a dog is good or not or a naughty dog or not, we're going to give him one treat 80 00:06:19,390 --> 00:06:20,350 ‫or two treats. 81 00:06:22,530 --> 00:06:27,720 ‫So now let's go over to our main method, because that's where our starting point of our application 82 00:06:27,720 --> 00:06:28,130 ‫is. 83 00:06:28,140 --> 00:06:31,230 ‫And at this point, let's go ahead and create a dog shelter here. 84 00:06:31,530 --> 00:06:31,860 ‫All right. 85 00:06:31,860 --> 00:06:36,210 ‫So we have this dog shelter called Shelter, and it will be a new dog shelter. 86 00:06:36,210 --> 00:06:42,390 ‫So we initialize it straight away as well so that we can use this shelter object because the shelter 87 00:06:42,390 --> 00:06:44,820 ‫object will contain a list of dogs. 88 00:06:44,820 --> 00:06:45,330 ‫Right. 89 00:06:45,420 --> 00:06:50,970 ‫So now what we want to do is we want to iterate through the list and check if we want to give them treats 90 00:06:50,970 --> 00:06:51,540 ‫or not. 91 00:06:51,750 --> 00:06:58,220 ‫So let's create a for each here like so double tab in order to prepare it for us. 92 00:06:58,230 --> 00:07:01,320 ‫So what is it that we are going to go through? 93 00:07:01,350 --> 00:07:04,470 ‫Well, we are going through a dog. 94 00:07:05,750 --> 00:07:07,610 ‫And we're going to call a dog. 95 00:07:07,610 --> 00:07:10,780 ‫So every single item inside of the list will be a dog. 96 00:07:10,790 --> 00:07:16,700 ‫And then the collection that we want to go through is, in fact, going to be the shelter. 97 00:07:18,060 --> 00:07:18,150 ‫Like. 98 00:07:18,240 --> 00:07:20,400 ‫So now we have a little problem. 99 00:07:20,400 --> 00:07:26,160 ‫Once we hover over it, we say we see here name of collection or array to iterate through is the shelter, 100 00:07:26,250 --> 00:07:32,190 ‫but the for each statement cannot operate on variables of the type dog shelter because dog shelter does 101 00:07:32,190 --> 00:07:38,250 ‫not contain a public instance or extension definition for get enumerator. 102 00:07:38,580 --> 00:07:41,340 ‫Okay, so there we have this get enumerator. 103 00:07:41,760 --> 00:07:43,350 ‫So what does that mean? 104 00:07:43,590 --> 00:07:53,550 ‫Well, this means that we need to implement the interface I enumerable right here for our dog shelter 105 00:07:53,760 --> 00:07:56,640 ‫because this dog shelter has a list that we want to iterate through. 106 00:07:56,640 --> 00:07:56,880 ‫Right? 107 00:07:56,880 --> 00:08:03,570 ‫Because if you look at it, shelter, well, our dog shelter, that's the shelter that we want to iterate 108 00:08:03,570 --> 00:08:04,050 ‫through. 109 00:08:04,500 --> 00:08:11,970 ‫And in order to make it iterable, we need to implement the AI enumerable here. 110 00:08:12,270 --> 00:08:16,110 ‫And what type of objects do we want to enumerate? 111 00:08:16,350 --> 00:08:19,740 ‫Well, in our case, it's going to be the dogs. 112 00:08:20,070 --> 00:08:26,010 ‫So we want to be able to go through the dogs inside of the dog shelter because, well, we have this 113 00:08:26,010 --> 00:08:30,960 ‫list dogs and it's initialized here in the dog shelter constructor. 114 00:08:31,020 --> 00:08:36,090 ‫And we want to make sure that we can just go through all of them and do something with the dogs, like 115 00:08:36,090 --> 00:08:39,480 ‫pet them or in our case, give them a treat. 116 00:08:40,110 --> 00:08:44,640 ‫So once you implement this and you go up here, the error disappears. 117 00:08:44,640 --> 00:08:51,360 ‫So suddenly it's fine because, well, our shelter, which is a dog shelter, now follows the instructions 118 00:08:51,360 --> 00:08:55,410 ‫that were just given to us that it needs to implement it. 119 00:08:55,410 --> 00:08:56,730 ‫I enumerable here. 120 00:08:57,420 --> 00:09:00,900 ‫So this is the generic I enumerable because we have this doc right here. 121 00:09:00,900 --> 00:09:01,200 ‫Right? 122 00:09:01,320 --> 00:09:02,700 ‫Because it's not this one. 123 00:09:02,700 --> 00:09:06,240 ‫It's the one with the type that we're giving to it. 124 00:09:06,450 --> 00:09:09,510 ‫But now, once we hover over it, we see we get another error. 125 00:09:09,660 --> 00:09:13,470 ‫It says doc shelter does not implement interface member. 126 00:09:13,470 --> 00:09:22,380 ‫I enumerable doc get enumerator and the I enumerable get enumerator so we can manually put them in there. 127 00:09:22,380 --> 00:09:29,850 ‫But the beauty about using an ID is that the ID can do a lot of work for us, so you can click on show 128 00:09:29,850 --> 00:09:37,530 ‫potential fixes and then click on implement all members explicitly and it will then add the collections 129 00:09:37,530 --> 00:09:44,310 ‫namespace for us and it will take care of adding those two methods for us or those two members. 130 00:09:44,370 --> 00:09:47,460 ‫So once we click on this, you see that this error will disappear. 131 00:09:47,460 --> 00:09:56,370 ‫And if we scroll further down, you will find that now we are returning an enumerator of type doc and 132 00:09:56,370 --> 00:09:59,910 ‫a non generic I enumerator collection. 133 00:10:00,300 --> 00:10:02,550 ‫So now we haven't implemented it. 134 00:10:02,670 --> 00:10:07,920 ‫That's why we have this throw new not implemented x exception here. 135 00:10:07,920 --> 00:10:10,350 ‫So we need to change that here. 136 00:10:10,350 --> 00:10:16,470 ‫We can now implement what should happen with our get enumerator method once it is called. 137 00:10:16,770 --> 00:10:22,470 ‫So in order to fix it, what we're going to say here is just return. 138 00:10:24,120 --> 00:10:30,660 ‫Dogs our list dogs dot get enumerator just like so. 139 00:10:31,470 --> 00:10:40,200 ‫And that will be the implementation because our dog shelter is now implementing the enumerable interface. 140 00:10:40,440 --> 00:10:48,810 ‫And our list here is, in fact, A, if you hover over it, a list is a generic list. 141 00:10:50,100 --> 00:10:58,590 ‫And now we can take these list dogs and we can get the enumerator for every single iteration that our 142 00:10:58,590 --> 00:11:00,230 ‫for each group will go through. 143 00:11:00,240 --> 00:11:03,930 ‫So basically it will return the individual dogs. 144 00:11:03,930 --> 00:11:09,990 ‫And now what we can do is we can actually use the dog in our for each loop here. 145 00:11:10,720 --> 00:11:18,260 ‫So that basically means that we can now go ahead and check if the dog is not naughty. 146 00:11:18,280 --> 00:11:22,420 ‫So not dog dot is naughty. 147 00:11:23,360 --> 00:11:30,650 ‫If that's the case, that basically means if the dog is not naughty, we are going to call the method, 148 00:11:30,680 --> 00:11:34,610 ‫give treat, and we're going to give him two treats. 149 00:11:35,330 --> 00:11:41,690 ‫And otherwise, if he was naughty, because we can't not give treats, we just love dogs too much. 150 00:11:41,900 --> 00:11:46,250 ‫We are just going to give them one treat like so. 151 00:11:50,000 --> 00:11:52,640 ‫And at this point, we can run our application. 152 00:11:54,790 --> 00:11:56,170 ‫And we will see. 153 00:11:56,200 --> 00:12:02,980 ‫Casper said Wolf two times, because he's a good dog, sniff is a naughty dog, so he gets only one 154 00:12:02,980 --> 00:12:09,550 ‫treat and he says, woof, once Oreo does it twice and pixels move twice as well. 155 00:12:10,480 --> 00:12:10,960 ‫All right. 156 00:12:10,960 --> 00:12:19,600 ‫And that is basically how you can make your class follow the instructions or implement an AI enumerable 157 00:12:19,600 --> 00:12:27,400 ‫so that you can loop through a list using the for each loop, for example, as we have it here. 158 00:12:28,180 --> 00:12:35,290 ‫Now, you might wonder, why are we not implementing the gate enumerator with the AI enumerable, the 159 00:12:35,290 --> 00:12:37,480 ‫one that is non generic? 160 00:12:37,510 --> 00:12:43,540 ‫Well, that's because we don't use a non generic example and our dog shelter is not non generic either, 161 00:12:43,540 --> 00:12:51,460 ‫but it's just that you need to implement it when using the AI enumerable interface because otherwise 162 00:12:51,460 --> 00:12:55,450 ‫if you get rid of it, we will get a warning here again. 163 00:12:56,910 --> 00:13:02,490 ‫You see here, we're actually in error, so we need to make sure that we have this method in there as 164 00:13:02,490 --> 00:13:03,030 ‫well. 165 00:13:04,170 --> 00:13:04,770 ‫All right. 166 00:13:04,770 --> 00:13:06,390 ‫So that's it for this video. 167 00:13:06,390 --> 00:13:14,490 ‫And the next video, we are going to look at another example where we are going to take advantage of 168 00:13:14,490 --> 00:13:16,670 ‫the eye, innumerable even more. 169 00:13:16,920 --> 00:13:18,980 ‫So this was just a little bit of an introduction. 170 00:13:18,990 --> 00:13:24,450 ‫I know this topic is rather complex and complicated, but we're going to take advantage of them a little 171 00:13:24,450 --> 00:13:24,930 ‫more. 172 00:13:25,110 --> 00:13:29,250 ‫And now, by the way, just a quick one, just a summary here. 173 00:13:29,280 --> 00:13:33,180 ‫When is it recommended to use the AI enumerable interface? 174 00:13:33,330 --> 00:13:39,030 ‫Well, you should use it when your collection represents a massive database table. 175 00:13:39,030 --> 00:13:46,470 ‫So you don't want to copy the entire thing into memory and cause performance issues in your application 176 00:13:46,560 --> 00:13:48,500 ‫because the database could be huge, right? 177 00:13:48,510 --> 00:13:51,210 ‫It could have millions upon millions of entries. 178 00:13:51,210 --> 00:13:56,400 ‫And you don't want to have to load the whole thing to iterate through it, but you just want to load 179 00:13:56,400 --> 00:14:01,920 ‫as much as you require for the current process, whatever you want to execute. 180 00:14:01,920 --> 00:14:05,880 ‫And then when should you not use the AI enumerable interface? 181 00:14:05,970 --> 00:14:13,530 ‫Well, when you need the results right away and you are possibly mutating, editing the objects later 182 00:14:13,530 --> 00:14:14,070 ‫on. 183 00:14:14,070 --> 00:14:17,700 ‫In this case, it is better to use an array or a list. 184 00:14:17,700 --> 00:14:23,820 ‫So when you want to be able to mutate or edit objects later on and you don't need the result straight 185 00:14:23,820 --> 00:14:25,710 ‫away, use an array or a list. 186 00:14:25,860 --> 00:14:30,510 ‫Otherwise use an AI enumerable because it's going to be more efficient. 187 00:14:32,040 --> 00:14:32,400 ‫All right. 188 00:14:32,400 --> 00:14:33,600 ‫So that's it for this video. 189 00:14:33,630 --> 00:14:34,680 ‫See you in the next one.