IEnumerable 🆚 IEnumerator Interfaces in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up youtube this is dennis panyuta for tutorials.eu in this video you are going to finally understand what i enumerable and i enumerator are they are interfaces but you are going to understand a little more about when to use them how to use them what the advantages of using them are and how they are related to lists then arrays and other types of collections so you will understand collections a lot better after this video it's definitely something you should have in your arsenal when you are ac sharp developer so definitely check out the video until the end and fully understand it and of course leave comments with ideas if you have any and hit that like button if you like the video alright let's get started in this video we're going to look at some very important interfaces that you should understand once you want to become a more advanced sharp developer and that is the innumerable interface as well as the ienumerator interface okay 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 the interface video that we have to offer as well okay so the ienumerable 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 innumerable interface so the innumerable interface in simple english when a collection class implements the i innumerable interface it becomes countable and we can count each element in it individually so there are two different versions of the i innumerable interface one is that works for generic collections why you have this innumerable opening bracket t closing bracket and then the i enumerable interface for non-generic collections so first of all we need to understand what collections are like arrays collections are classes that we can use to store many objects at the same time or a collection of objects hence the name collection unlike arrays collections are not limited to one type of object so instead in a collection we can store as many types of objects as we want also collections are not fixed in size like arrays for example with by their length we can add elements to them as we go and the collection will adjust its size in this case it's not called length but count and we're going to see how to use that so why do we need collections well let's imagine this scenario where we are building an attendance system for a school and we want to add students to our system dynamically over time once we define our student class and start creating objects of the type student we will face an issue how are we going to store them while storing them in an array is a bad idea for a few reasons first of all arrays are fixed in size so if we create an array of 500 students then we can't have any more than that secondly what if we decide to add other types of objects to our system like teachers employees and so forth well since an array is fixed in size and can only accept one type we can use collections to solve the issues so we use them to store manage and manipulate groups of objects more efficiently so collections can be used to do a lot of operations in a group for example adding deleting replacing objects searching for particular objects and even copying objects so now there are two different types of collections non-generic collections which we can use to store any type of object and they are inside of system.collections inside of that namespace and then we have the generic collections which is limited to one type of object so we use them if we are sure that we don't need different types of objects that we want to store and they are located in system.collections.generic so in that namespace so non-generic collections let's look at some examples so here we have this int num equal 1 is 5 then we have this float num2 is 3.15 string name is for example dennis and then let's create this new arraylist my arraylist and we add num1 to it then we can add num2 to it and we can add a name to it so you see here that they're non-generic which means it allows us to use different types so one type was an integer the other type was an float so num2 was a float and the name was a string so we can add all of those to our arraylist they're of different type so you see we're using the add method in order to add them to our list now we could use the methods that we were talking about before which was copy delete replace and so forth we could use them in here or with this arraylist because an arraylist is a non-generic collection and now we can go through all of the elements in my arraylist using the for each method and because it's not of one particular type so the elements inside of our arraylist are not of one particular type we need to use object element in my arraylist we cannot use int because there are floats and strings in there as well we cannot use float because there are instant strings as well so that's why we're using object which is a very generally used type that allows all kinds of types the object type is basically incorporating all other types so now let's look at the generic collection so this one will have animal one animal two animal three and it will be of type list and we're going to call it my list so you can see this list can only store strings so we're creating this strings list and since the list is a generic collection it can really only hold one type of object so we have to specify that type when defining that list so here we are specifying it's of type string so the data that we can pass to it or save in it or store in it will only be strings by giving the type between an angle brackets in this case here you can see angle bracket string like this so we technically would say that my list is a collection of type list string in upcoming videos of this chapter we will explore different types of generic and non-generic collections and we will discuss the benefits of each one of them and how and when to use them okay so this was just a very quick introduction to the two topics we're going to see a bunch of examples and then it will become a lot more clear quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c sharp master class in which you're going to learn all of the things you need to know about c sharp so you're going to learn how to do the basics how to use object oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below 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're basically an improved version so to speak but so if you are creating a new project it's recommended to use the generic version because an i innumerable 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 innumerable t interface so the generic collections interface okay 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 i innumerable and the i enumerator you maybe have heard those before so the i 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 little more sense so the get enumerator method is the one that you need to implement and it returns an i enumerator object okay so this method that you need to implement when you want to use a i innumerable for your class or when you want to implement it for your class you need to have this get enumerator method which then returns an i enumerator so the i 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 okay this is a lot more complicated than it will be once we actually run this in our particular example okay 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 okay so if he's not a naughty dog he will get less treats than if he's a good boy or a good dog okay 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 that the dog said woof so for every single treat that the dog is given he will say okay so basically if he's a good dog he 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 dog 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 okay 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 okay 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 a naughty dog but sif is a naughty dog or rio 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 stocks 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 logs 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 are going to call it 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 innumerable right here for our dog shelter because this dog shelter has a list that we want to iterate through right because if we 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 ie numerable 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 arrow 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 the eye innumerable here so this is the generic innumerable because we have this dog 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 arrow it says dog shelter does not implement interface member innumerable dog get enumerator and the i innumerable get enumerator so we can manually put them in there but the beauty about using an ide is that the ide 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 arrow will disappear and if we scroll further down you will find that now we are returning an i 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 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 like so that will be the implementation because our dog shelter is now implementing the innumerable interface and our list here is effects a if you hover over it a list is a generic list okay 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 gift 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're just going to give them one treat okay like so and at this point we can run our application and we will see casper said move two times because he's a good dog sif is a naughty dog so he gets only one treat and he says woof once or rio does it twice and pixel says move twice as well all right and that is basically how you can make your class follow the instructions or implement an i innumerable so that you can loop through a list using the for each loop for example as we have it here okay now you might wonder why are we not implementing the get enumerator with the i 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 i enumerable interface because otherwise if you get rid of it we will get a warning here again you see here or actually an arrow so we need to make sure that we have this method in there as well alright so that's it for this video in the next video we are going to look at another example where we are going to take advantage of the ienumerable even more okay 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 i 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 i innumerable 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 results straight away use an array or a list otherwise use an i innumerable because it's going to be more efficient in the last video we looked at how the i enumerable works from a technical point of view in this video we are going to check out how we can take advantage of it because the cool thing of i innumerable is is that the list types that we use usually such as list queue array all of those are in fact implementing the innumerable interface so this gives us a lot of freedom and we're going to see what we can do with it so i'm going to have this method called getcollection and what it will do is it will return an i enumerable of type integer okay so we will need to return something at one point so let's return null for now to get rid of this error here and now if we want to use the i enumerable we can see that we need to add system collections generic because here we're using integer we're going to return an iron variable integer so that means we need to make sure that we have this namespace collectionsgeneric all right so now what shall this getcollection do so basically we can have multiple options so if we select the option1 our git collection will return a list if we take the option 2 it will return a queue and if we take any other option such as 3 4 5 whatever it will return an array of integers so this get collection method will return some entirely different types of lists depending on the option that we're going to pass to it so that's the idea behind this get collection and that's also the power of using the i innumerable interface because as i said list q and array are all implementing the i innumerable interface okay so let's look at it let's create this list which i'm going to call numbers list and i'm going to initialize it straight away so it will be a list of integers and the numbers will be one two three four five and then we're going to create a queue with a bunch of values so here's 6 7 8 9 10 and we're going to enqueue them to this numbers queue so you see q numbers queue now if you hover over it you can see it's also a generic collection okay same goes for the list it's a generic collection same goes for the ienoble generic collection so you can see there is a certain connection already so now what i'm going to do is i'm going to check what are the options that are taken so what is passed to my get collection method if it is 1 then i'm going to return my numbers list if it's going to be 2 i'm going to return my numbers q so like an entirely different type of list okay so it's containing different type of information and different values but it's really just an entirely different thingy okay so it's not a list it's a queue and a queue is not a type of a list so q is not inheriting from list or something like that okay so that's really the interesting thing here what we can do and otherwise so if the option is anything else we're going to have this else case where we are just going to return 11 12 13 14 50. now in this case you can see this return statement here is never going to be reached so we can get rid of this line here entirely because it's basically never going to be reached if we have this if else statement with a bunch of returns here all right so now that we have this get collection method let's actually use it in our main method so here in the starting point of our application let's create an unknown collection so just an i innumerable collection that is going to be a generic collection that uses integers or holds integers and we will use this variable to store the collection that will get returned by the get collection method so by this method that we just created now let's actually just assign get collection one to it so because we passed one to it unknown collection will now hold and let's look here the list here this list here not the queue but the list okay so that's a really cool thing so now we can display this on our console saying for each number in the collection we got back from the get collection so we're going to use two for each in order to get all the numbers that are in our unknown collection because if something is an i innumerable so if a list type like a list like an array they can queue is of type or is implementing the i enumerable interface then we can iterate through it using it for each loop which is what we're doing here so we're going to go through this unknown collection and for each number we're just going to write it onto the console okay so let's run this real quick and we can see one two three four five was printed out so that seemed to have worked now let's do the same thing but with collection two so get collection and we pass the option of two which will now return the queue but it will store it in the base type of our generic collections okay so unknown collection is the base type i enumerable which is basically the base type of our list and our queue so now we can just assign it to it because unknown collection doesn't care if it's a list if it's a queue if it's an array because all of them are following the instructions of an i enumerable so they followed the implementation of it so now if we run this code now it will display the queue here six seven eight nine ten so this was a q int and it displays it for us all right and now let's look at this if we enter any other type of option so let's say we give it the option five which will be returning this else case here which basically will return an array of numbers okay so let's run this again and we will see that now it returns 11 12 13 14 15. so of course these were the ones from before so these four each loops okay we didn't delete them of course we could get rid of them to see that now it only will do the array or display the array so there we are it only displays the array that's it for this example you see that we have quite an advantage of using i innumerable so the base type so to speak of any type of generic list that we want to make iterable like the for each loop well e for each loopable so to speak and that's really the power of it really useful stuff in the next video we're going to look at another example where we're going to pass an i innumerable collection as a parameter for a method quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c master class in which you're going to learn all of the things you need to know about c sharp so you're going to learn how to do the basics how to use object oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below welcome back in this video we're going to look at i enumerable one more time and it's another demo where we're going to see how we can pass an i enumerable to a method as a parameter and this gives us a lot of flexibility because well similar to what we have when it comes to inheritance in object-oriented programming where the instance of a child class can be passed the same way the parent would be passed into a method call for example as the parameter something similar happens when we are working with collections that implement the i enumerable interface as we have seen in the last video where we could replace the i enumerable so to speak with lists with cues as well as with arrays okay so let's go ahead and just create a new method here which will in fact take an i enumerable collection as the parameter okay so i'm going to put that down here and as you can see we're using the i innumerable which is of course going to require the generic collections namespace up here all right now we can use it throughout our file here and what i'm going to have is basically a sum that will store the sum of the numbers in any type of collection so in our any collection and then we're going to go through each item in the collection and add that together and put it into some so basically summing up all of the values that are inside of that collection and then we are just going to write out the result of the console so console write sum is and then the sum okay so that's the idea behind this collection sum now the cool thing is we can pass any type of collection to it that implements the i enumerable interface this means that we can pass a list we can pass an array so we have a bunch of options here okay so let's actually make use of that therefore i'm going to set up a list like so in my main method okay list number list so this will just be a list of numbers then i'm going to do the same thing with the array so i'm going to create a number array of integers then i have a new line well i'm creating a new line using this rightline statement and i'm going to call my method collection sum where i'm going to pass the numbers list which is this list full of numbers 8 6 and 2. okay so if we pass that and then run the program it will display the result of 8 plus 6 plus 2 which is going to be 16. all right and now the cool thing is we can of course do the same thing with our numbers array like so so here we are passing the number array which is an entirely different type right so this is not a list this is an array and we're just passing it in and if we run it we will see that it will work the same way so it's going to say sum is 12 and that is the sum of 1 plus 7 plus 1 plus 3. so that's really the advantage of using the generic type i innumerable here so basically the base type you could see it's not really the base type of a list right but the list class implements the i innumerable so if you go over to list here you see it follows the instructions of the i innumerable both the generic as well as the non-generic options here as you can see and that's really the cool thing here that's really the power of using i innumerables so yeah that's it for this video now i hope you have a certain idea of what you can do now with the i innumerables and whenever you come across the high innumerable collection type you will not be confused because they are used quite a bit in programming all right so now you should be a pro in collections and fully understand what eye innumerable and innumerator does for you and how you can use it because those were topics that i had struggled with when i began programming as well and i thought it would be a good idea to share the knowledge about it with you to well make your life a lot easier when it comes to working with collections in the future so i hope you enjoyed it and well if you did then please leave a like and also if you loved the video and you want to learn a lot more about c sharp definitely check out the c sharp master class you can find the link in the description below and if you haven't subscribed already please do so now because it really helps us out and it will help you out in the long term as well because you are going to get notified or at least recommended a bunch of our videos in the future which we are working on constantly to create the best possible c-sharp as well as unity content okay so for now check out one of those two videos and have a nice day
Info
Channel: tutorialsEU
Views: 5,992
Rating: undefined out of 5
Keywords: Tutorials, Tutorial, Programming, Course, Learn, guide, development, programmer, video course, video tutorial, learn how to, how to, what is interface in c#, ienumerable vs ienumerator C#, implementing ienumerable and ienumerator, ienumerator in c# example, ienumerator in c# geeksforgeeks, use of ienumerable and ienumerator in c#, c# interface tutorial, implement interface C#, why we use ienumerable in c#, c# implement multiple interfaces, c# implement interface, C# collections
Id: VcAubtFBOdY
Channel Id: undefined
Length: 34min 6sec (2046 seconds)
Published: Tue Apr 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.