Enumerables (IEnumerable, IEnumerator) | C# Programming Tutorials Beginners: 17

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in this episode we're going to talk about enumerators specifically the ienumerator and ienumerable interface and what makes them different from arrays so the first step in understanding link queries which is the ultimate goal here is to understand ienumerable which is the first step in the process after that will in iqueryable and then we'll show you some link expressions so I want to first take a look at the higher numerable and what makes it different than simple of race and to understand what actually happens when you remunerate over something that it isn't happening instantly it happens at the time of enumerating to the next object so I should jump into visual studio and start the project off and take a look so I've got here just a brand new console project to start looking into the enumerables so in order to talk about first what an enumerable is let's just do something that all of you have probably done which is to create an array so we just do say a new integer array and let's just put 1 2 & 3 m so this is a standard array I'm sure you all know what these are and then usually if you want to iterate over them or enumerate you know basically go through the list if you will go through each one and do something with that's typically called enumeration you go in through a list and you know get any charge of and then doing something with it so in code you dip to do for each and then you can say for each for a in array and then console.writeline say a is and then just output the current value and then just so we don't close here we'll just do read read align and then we have to press ENTER to close so if we run this code now you'll see we're basically enumerator with this array we go through 1 2 & 3 and output the value so this for each or as you might see it written as well like for VAR i equals 0 eyes less than array dot length i plus plus and then you do console.writeline a is and then you get it via indexing so by array and then the index position so you can do it either way obviously the the typical way prefers the for each unless you need the actual array position so that's how you enumerate over an array and what you might not know is an array is already an ienumerable and that's why this for each works so a for each works on any enumerable so before we get into creating our own enumerable let's first take a look at how and why this is already an enumerable interface so we type array dot and you'll see it's got getenumerator so we type that just so we can click on it and press f12 it will take us to this method the states get enumerator and it returns an IU Miraval you can see we're in the array namespace and if you scroll up or in that a class array and it implements an ienumerable and this is the thing we're talking about in this video now so it totally implements collections lists a bunch of other things but specifically taking one piece at a time we're talking about ienumerable so an array already implements that and if we go to an ienumerable and look at the interface it's simply asked to implement this one function which is to get any numerator for this enumerable so this enumerable is something that can return an enumerator that can enumerate over values so Matt sound a bit confusing than many but you'll see how it comes together so all we have to do is to use a for each the object in the for each so in this case that's array most implement ienumerable if it doesn't we can't use for each but because it does this statement basically compiles down to the exact same thing as doing this so you'd say for enumerator you do equals array get enumerator and then you do wael numerator don't get next or move next rather and you can see it returns a boolean so true if there's another item to be had if you will so while it can get one you can now do the console.writeline and this time instead of being a it's the enumerator dot current so get whatever the current value is so this bit of code here has effectively synonymous to this code that's basically all this for each dose so you keep that in mind this is the same code if you will so can we put a numerator a is just so we've got the same you can see the same output effectively to see the same thing happening it just enumerates over the array so that's what we're basically doing in a for each statement and that's that's what any numerator does so this getting numerator obviously just gets an enumerator that's what they're the method has to implement well now the ienumerator has these three a property and two methods basically so you call move next to save right let's try and get the next value in this enumerable list so in this case it would get the first value here it would be turned successful and then once it's successful at the end of that call this current should point to whichever object we're currently on as we step through and then as your call move next again it should set the current to this value and then the dot current will let me turn this value so that's all the array is doing and that's all you know this before each does is replace it with this so with that in mind the next thing then is let's just create our own innumerable and to see and you should also notice you as well which is kind of important not necessarily in a raise because they're already in memory the values already exist but it's possible in an enumerable and in an enumerator to generate the values on the fly so as you're stepping through you're not necessarily reading all the values up front and then iterating through them you might simply be generating them on the fly at that point in time which is typical of an enumerator when you're accessing a database and you select the first say million rows of a database and then you want to step through them until you find the first one with a certain name you wouldn't want to read a million entries into the memory and then step through them in code so an enumerator allows you to do that it doesn't actually state that you know at that point in time there's any predefined end you can't say on the enumerate to the length so if you've got an enumerator you can see there is no link there's no known you don't know when it's going to end you just have to keep calling move next until it fails the reason an array has a length is if you press f12 on length you'll see it takes you to an actual property in the class array itself so the length property is defined in the array and not necessarily in the enumerable so enumerables can be infinitely long and also only generate the results at the time have been asked for them here so let's do that so you can see so let's just go below the main class and just do a public class and call it my infinite enumerator or my infinite enumeration well which way is it actually it is enumerable so my infinite enumerable will keep the name in the same and this will implement which scroll down I enumerable and then we click on here control dots it will add the name space using system duct collections and we control click so a control dot on it again and press enter it will tell us we need to implement this interface so in our case we're just going to return a new my infinite a numerator which will now go and create so that's all we have to do to have a value that we are innumerable over so like the array object so we're cloning this array objective ul and now we need the thing to step over the values so we do public class my infinity numerator and that implements I enumerator you can see again if we do control dots and enter it will throw is these three things you need to implement so current can be in this case I get an a private set and we'll set it to zero and move next needs to effectively now get the next value well in this we haven't passed any values in we're going to just generate them on the fly and also going to do is infinitely increment less integer just to show you the you know the example and the point of it at this point in time is when you know the work happens in the code can generate the next value so we'll just say the current value increases and we always have another value so we are infinitely long this also because we are specific type instead of just being generic here you can do any numerous numerator of a generic type so we'll say we're specifically an integer control dot on that sits in different namespaces in generics and then the same for the SIA that's let's define that we are an integer and we implement that interface you can see we have to return two so we just return the same thing for both we now honor the generic version of it so it gives us a specific type here so this could be now int as the type the interface will be slightly different there so we just implement the interface you'll see that we have a dispose because when our generics it's it's adding to dispose we don't need to do anything though and it's added the underlying in numerator current which will just return the generic specific type of current so there's not much difference there we basically just added you know a generic type so we can say specifically we're expecting this enumerable type to be integer so that the objects coming out of the enumerator are always integers as now the current plus plus allows us to increment that integer and we've always got the next one reset we could implement so we expect that to say set current to zero and that's basically it right there so if we wanted to know do a for each on that we could go here and do the same thing so we could save our infinite enumerator or innumerable cracky these words correct there's new my infinite innumerable and you see we're not passed any values in because in this case the purpose of this is to show that we're just generating some infinitely long values on the fly it's not given at any point in time and then from that you could just do for each VAR i in infinite innumerable and then console dot write line i is and then output i and what you should see now we run this code we should just get an infinite loop of numbers coming out so you can see that's happening there now so this is now an enumerable that will go effectively forever it will just go all the way to int 32 max and then all overflow go back to zero and carry on to this I'll never end that let's always keep going and what that's done if you remember from the example above here so instead of for each if we did exactly the same here so we were to replace the for each with the enumerator so the enumerator could be the infinite innumerable getting numerator which because we have implemented innumerable doctors here so when you enter a for each loop the first thing it does is say well based on this value we've given it get an enumerator for it that gives it this class which is the enumerator and now to effectively enumerate over that list we call the move next so in the move next it comes in and increments current i1 and then it returns true so the very first value will come out will be one because it started at zero but the has to call move next to initiate there you know to know if there's even anything in this there might be zero items so it has to call it first so our first value in this list would be one and that will be the move next with your name true and then at that point in time we say okay get me the current and the current at that point in time because we have set it effectively be existing and when we move next we just increment that value we're simply getting that value so if we were to run this and remove the for each so you can see it being enumerated that way you'll see it does exactly the same thing it enumerates over so that really is what an enumerator is and it's really simple like most other things it seems like it might be complicated it seems like if you're doing an array and you do a for each to some kind of magic going on or you've got now an enumerable and you just do for each one and you're not fully sure what's going on this is all that's going on so this is the entire thing right here that's all that's happening you you're simply having an object that can be anything you like this class could do and be anything it liked at all and it simply implements this interface ienumerable which has to return an enumerator so that's all the first step does is to tell the for each loop that it can say here's the thing that's going to be numerate over my values and typically in here when you're returning a enumerator you're passing the values at that point in time so this contained a list of value set so if we wanted a private let's just do an array so it's still a private to int array and let's just make it and my data a new array and let's do four or five six let's just have a predefined hard-coded set of data in this class and now 12 hours to enumerate over that data we would normally pass that in to whatever enumerator you know wants to go through our values so with that now we need the constructor to accept an array events and then we want to store them so we just do a private enter a and values and it will be M values equal values so now we have the values passed in current can then be the offset so we'd also have to have the index position I would say index starts at we start at -1 when we do move next which can now just implement increment the index position will then start at 0 correctly and now instead of returning true here we simply return if the index value is less than the values length so the length is ones one item in there 0 is less than it as you move to the second item in the list this would be 1 1 is not less than 1 so therefore it's out of range so this will now move next until it gets to the end of the array the values assert and the current now we need to simply update to be the M values M index so at any point in time we'll basically get the value at the current index array so if we change it this way around which is more conventional this is how you typically pass in some data and if we were to now enumerate over our list you can see now it's actually one two three four five six why is that oh that's because let's just clear up the old array let's get rid of the array there we go keep it clean so we don't get confused there there's just this one now and the infinity numeral the O is four five and six and then it stops so that's how you can also pass in specific data or simply turn whatever class has this data into an enumerable so it can be for each Tovar and the next step from this now is to go on to ultimately using link expressions to query objects and query lists of data easier but hopefully that's all you need to know about enumerators there's not really much to say we are basically just looping a bunch of values and it's just the way in which we do them by getting an enumerator which then does whatever it wants through these three calls to simply do move next to set up whatever Daedric needs to calculate the next piece of information and set it ready and store it in current and then the for each loop can get the current and the loop can just continue and that's really all of us to it it's it's a loop of move next get the current value move next get the current value until there are no more values that that's ultimately all it is so hopefully that was you know a useful explanation of enumerators so that's I enumerables in a nutshell there's nothing much to them hopefully now you understand them better than you did before and you can make you something where you need as always have you got any questions or comments just ask them and I'll see you in the next one [Music]
Info
Channel: AngelSix
Views: 84,482
Rating: undefined out of 5
Keywords: c#, beginner, tutorial, ienumerable, enumerable, enumerator, ienumerator, class, inheritance, derived, sealed, casting, enum, guide, parsing, string, int, convert
Id: UfT-st9dl8Q
Channel Id: undefined
Length: 18min 16sec (1096 seconds)
Published: Mon Dec 31 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.