‫And this video, we're going to look at some very important interfaces that you should understand once
‫you want to become a more advanced shop developer.
‫And that is the AI enumerable interface as well as the AI enumerator interface.
‫So you should understand interfaces before watching this video, because otherwise some of the concepts
‫won't make sense to you potentially.
‫So I really recommend to check out that the interface video that we have to offer as well.
‫Okay.
‫So the AI enumerable interface is the base interface for many collections in C sharp and its job is
‫to provide a way to iterate through a collection.
‫So this is why we can use for each loops to go through a list or a dictionary because they are inheriting
‫from the enumerable interface.
‫So the enumerable interface in simple English, when the collection class implements the AI enumerable
‫interface, it becomes countable and we can count each element in it individually.
‫So there are two different versions of the AI enumerable interface.
‫One is that works for generic collections where you have this AI enumerable opening bracket, closing
‫bracket, and then the AI enumerable interface for non generic collections.
‫So you should understand what generic and non generic collections are as well.
‫They have certain advantages and disadvantages, but I have a video for that as well.
‫I hope you watched it before watching this video because otherwise it's a little complicated.
‫Okay.
‫So these two that we have here, let's look at those.
‫Since the generic collections were introduced after the non generic collections, it's recommended to
‫use the generic collection.
‫So they are basically an improved version, so to speak.
‫So if you are creating a new project, it's recommended to use the generic version because an I enumerable
‫for non generic collections needs to apply or perform boxing and unboxing, which basically is the conversion
‫of types of objects.
‫And we will later explain how to use the AI enumerable tty interface.
‫So the generic collections interface, we're not going to go over the non generic collections interface
‫because it's not recommended due to the boxing and unboxing, which just is less efficient.
‫So now let's look at the difference between the AI enumerable and the AI enumerator.
‫You maybe have heard those before.
‫So the AI enumerable interface contains a single method that you must implement when implementing this
‫interface, and we're going to do that later on in our particular example here.
‫So it makes a lot more sense.
‫So the get enumerate method is the one that you need to implement and it returns an AI enumerator object.
‫So this method that you need to implement when you want to use a AI enumerable for your class or what
‫you want to implement it for your class, you need to have this get enumerator method which then returns
‫an AI enumerator.
‫So the AI enumerator.
‫Provides the ability to reiterate through the collection by exposing a current property that points
‫at the object we are currently at in the collection.
‫This is a lot more complicated than it will be once we actually run this in our particular example.
‫So we're going to build an example fully understanding what all of this means.
‫So this is just to prepare you what you can expect.
‫Okay, so let's get started.
‫Finally getting away from all of this theory, let's actually look into practice.
‫So let's create this little class called dog.
‫And I'm going to put it in the same file to keep things simple.
‫So we have this class called Dog with a name and we have this Boolean called is Naughty Dog.
‫So we will basically check if the dog is naughty or not.
‫And based on that, we are going to give him treats.
‫So if he's not a naughty dog, he will get less treats than if he's a good boy or a good dog.
‫So the constructor here basically just sets those two values.
‫So when we create a dog object, we are going to give it a name and a boolean, whether the dog was
‫naughty or not.
‫And then we have this gift treat method and this will basically just print out the dog said Woof.
‫So for every single treat that the dog is given, he will say Woof.
‫So basically, if he's a good dog, you will get two treats and he will move twice.
‫And if he's a bad dog or a naughty dog, then he will only move once because he will get less treats.
‫Okay.
‫So now we have this class dock and we can now go ahead and create dog objects.
‫So let's have this other class.
‫Let's assume we have another class that will use dogs inside of a list.
‫So we have this dog shelter class.
‫So this dog shelter class will have a list of dogs.
‫So therefore we need to make sure that we have this using system collections generic because list is
‫a generic collection.
‫So here at the top you will see that now you have this using system collections generic.
‫Okay.
‫So now that we have this dog shelter and we have this list of dogs in the constructor of this dog shelter,
‫the only thing that we're going to do is to initialize this dog's list with a bunch of dogs.
‫So we have the Casper dog or a dog called Casper.
‫He's not an old dog, but if is a naughty dog, Oreo is not naughty and Pixel is also not naughty.
‫So don't ask me how we come up with these names, but that's basically how we're going to call those
‫dogs.
‫You could, of course, make this whole application a lot more complex, but our idea is really just
‫to use this list of dogs to iterate through the dog shelter, to go through the dog shelter, basically.
‫And based on whether a dog is good or not or a naughty dog or not, we're going to give him one treat
‫or two treats.
‫So now let's go over to our main method, because that's where our starting point of our application
‫is.
‫And at this point, let's go ahead and create a dog shelter here.
‫All right.
‫So we have this dog shelter called Shelter, and it will be a new dog shelter.
‫So we initialize it straight away as well so that we can use this shelter object because the shelter
‫object will contain a list of dogs.
‫Right.
‫So now what we want to do is we want to iterate through the list and check if we want to give them treats
‫or not.
‫So let's create a for each here like so double tab in order to prepare it for us.
‫So what is it that we are going to go through?
‫Well, we are going through a dog.
‫And we're going to call a dog.
‫So every single item inside of the list will be a dog.
‫And then the collection that we want to go through is, in fact, going to be the shelter.
‫Like.
‫So now we have a little problem.
‫Once we hover over it, we say we see here name of collection or array to iterate through is the shelter,
‫but the for each statement cannot operate on variables of the type dog shelter because dog shelter does
‫not contain a public instance or extension definition for get enumerator.
‫Okay, so there we have this get enumerator.
‫So what does that mean?
‫Well, this means that we need to implement the interface I enumerable right here for our dog shelter
‫because this dog shelter has a list that we want to iterate through.
‫Right?
‫Because if you look at it, shelter, well, our dog shelter, that's the shelter that we want to iterate
‫through.
‫And in order to make it iterable, we need to implement the AI enumerable here.
‫And what type of objects do we want to enumerate?
‫Well, in our case, it's going to be the dogs.
‫So we want to be able to go through the dogs inside of the dog shelter because, well, we have this
‫list dogs and it's initialized here in the dog shelter constructor.
‫And we want to make sure that we can just go through all of them and do something with the dogs, like
‫pet them or in our case, give them a treat.
‫So once you implement this and you go up here, the error disappears.
‫So suddenly it's fine because, well, our shelter, which is a dog shelter, now follows the instructions
‫that were just given to us that it needs to implement it.
‫I enumerable here.
‫So this is the generic I enumerable because we have this doc right here.
‫Right?
‫Because it's not this one.
‫It's the one with the type that we're giving to it.
‫But now, once we hover over it, we see we get another error.
‫It says doc shelter does not implement interface member.
‫I enumerable doc get enumerator and the I enumerable get enumerator so we can manually put them in there.
‫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
‫potential fixes and then click on implement all members explicitly and it will then add the collections
‫namespace for us and it will take care of adding those two methods for us or those two members.
‫So once we click on this, you see that this error will disappear.
‫And if we scroll further down, you will find that now we are returning an enumerator of type doc and
‫a non generic I enumerator collection.
‫So now we haven't implemented it.
‫That's why we have this throw new not implemented x exception here.
‫So we need to change that here.
‫We can now implement what should happen with our get enumerator method once it is called.
‫So in order to fix it, what we're going to say here is just return.
‫Dogs our list dogs dot get enumerator just like so.
‫And that will be the implementation because our dog shelter is now implementing the enumerable interface.
‫And our list here is, in fact, A, if you hover over it, a list is a generic list.
‫And now we can take these list dogs and we can get the enumerator for every single iteration that our
‫for each group will go through.
‫So basically it will return the individual dogs.
‫And now what we can do is we can actually use the dog in our for each loop here.
‫So that basically means that we can now go ahead and check if the dog is not naughty.
‫So not dog dot is naughty.
‫If that's the case, that basically means if the dog is not naughty, we are going to call the method,
‫give treat, and we're going to give him two treats.
‫And otherwise, if he was naughty, because we can't not give treats, we just love dogs too much.
‫We are just going to give them one treat like so.
‫And at this point, we can run our application.
‫And we will see.
‫Casper said Wolf two times, because he's a good dog, sniff is a naughty dog, so he gets only one
‫treat and he says, woof, once Oreo does it twice and pixels move twice as well.
‫All right.
‫And that is basically how you can make your class follow the instructions or implement an AI enumerable
‫so that you can loop through a list using the for each loop, for example, as we have it here.
‫Now, you might wonder, why are we not implementing the gate enumerator with the AI enumerable, the
‫one that is non generic?
‫Well, that's because we don't use a non generic example and our dog shelter is not non generic either,
‫but it's just that you need to implement it when using the AI enumerable interface because otherwise
‫if you get rid of it, we will get a warning here again.
‫You see here, we're actually in error, so we need to make sure that we have this method in there as
‫well.
‫All right.
‫So that's it for this video.
‫And the next video, we are going to look at another example where we are going to take advantage of
‫the eye, innumerable even more.
‫So this was just a little bit of an introduction.
‫I know this topic is rather complex and complicated, but we're going to take advantage of them a little
‫more.
‫And now, by the way, just a quick one, just a summary here.
‫When is it recommended to use the AI enumerable interface?
‫Well, you should use it when your collection represents a massive database table.
‫So you don't want to copy the entire thing into memory and cause performance issues in your application
‫because the database could be huge, right?
‫It could have millions upon millions of entries.
‫And you don't want to have to load the whole thing to iterate through it, but you just want to load
‫as much as you require for the current process, whatever you want to execute.
‫And then when should you not use the AI enumerable interface?
‫Well, when you need the results right away and you are possibly mutating, editing the objects later
‫on.
‫In this case, it is better to use an array or a list.
‫So when you want to be able to mutate or edit objects later on and you don't need the result straight
‫away, use an array or a list.
‫Otherwise use an AI enumerable because it's going to be more efficient.
‫All right.
‫So that's it for this video.
‫See you in the next one.