c# (Csharp) and .NET :- Difference between IEnumerable and IEnumerator.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we will talk about two important concepts I enumerable and I enumerator now many developers are confused to know what is the difference between I enumerable and I enumerator so what we'll do in this video is first let's see a small sample demonstration of both these fundamentals and then we will go ahead and chalk out the differences in order to understand both of these concepts you know what I have done is you can see that I have created a very simple list here this list is nothing but it's a list of integers and this list has you know some data for years years means like 1990 91 92 2001 2002 and 2003 so let's go ahead and create a very simple i enumerable on the top of this list so I'll go and say okay I want to create a ienumerable you know which is of type integer so I will just give a name as I enumerable ie num I can say and I'll say that okay convert I know this complete list you know to enumerable so I just say end and convert this list that is a years to I in available right I want to go and browse this a vertical enumeration what I can do is I can say for each int I in I mean um and I can just go and write down on the console screen so I can just say console dot write line i and if i do a control f5 you can see that i can see the list of years you know what I have in my list now we have written the code for enumerable now let's go ahead and write the code for a numerator and after that we'll go and find out the differences okay so I'm going to go and say okay I a numerator of int so this is I am right now you know the list right it actually has a nice function here which actually you know converts you know the list into a numerator so you can see that I am going to use that function get a numerator now in order to go and browse this enumerator which I have created the way I have to right now is it's very different than ienumerable so we to say why I enumerate dot move next so we have to do a move next and then we can say console dot write line I enumerate dot current dot two string right so that will help us to go and enumerate the enumerator so if I do a control f5 ok you can see that one enumeration was done by using the enumerable and the other values were displayed by using the enumerator right so in simple words both of these guys ienumerable as well as I am later helps you to loop through collections now these collections can be lists into it can be added lists or whatever it is so the next question has a developer which will come to your mind is so what should I use I'm going to see both of these guys are doing the same thing and if you watch very closely you will say that I will always use inm label because the syntax is short and sweet you know as compared to ienumerator if you look at ienumerator you have to do a move next you know then in order to access the current element you have to do a dot current and whatnot so you'll say that as a developer I will always use ienumerable why even I worry about Ino greater now before I start going into the differences you know let me just make one big statement here I enumerable you know is a sugar coating on ienumerator I say I enumerable actually uses I enumerator the whole point about ienumerable was you know to make the syntax shorter and sweeter so you can if you see that if you see the I in M variable object if you just CI enumerable and if you do dot right you can see that the ienumerable interface has a get a numerator function here and this get eliminated function actually gives gives you Backa i enumerator collection so in other words i enumerable uses i enumerator internally so that's the first point right now coming back to the main difference so that's good we understood it that ienumerable you know helps you to bring down the coding it helps you to basically what I call minimize your code you know and it's much shorter and sweeter you know as compared to using the ienumerator so now till now it almost be you have concluded saying that okay must I animal is better to use because it uses ienumerator syntax are lesser so why even bother about I enumerator now let me make one more statement the biggest difference between I am label and I enumerator is States I enumerable does not remember currently which row is he currently iterating through while I enumerator does what do I mean for example for now what I'll do I'm going to go and comment this code out okay now let's say you have two functions here let's say static Freud I'll say iterate from nineteen to twenty means you know iterate all the years you know from 1990 to 2000 okay so 1990 to 2001 and there's other function which actually iterates from must be 2001 and above okay so iterate thousand one and about right and what we would like to do is that okay this function iterate 19 to 2001 will iterate values from 1990 to 2001 1990 to 2001 while this function will take up the values from from ahead of that that means from 2001 to 2003 right and toubro this function let's go ahead and pass ienumerator so you can see that I'm going to pass him ienumerator here oh and also here I will pass ienumerator sorry int and into so this first guy what he will do is he will loop through the iterator so when we pass him the enumerator right from here it will actually loop through this enumerator here and display the values okay sorry oh that is oh but you know third time this this current value okay if it goes up of 2000 or 2001 we have to pass that iterator to the second function okay so I am going to say convert dot to int16 if the current value the o dot current if it goes above 2000 then please pass this iterator to the other function so that he can take away take from there and start doing the things okay and in the next function we'll just go and take the values from 2001 to 2003 and displayed so in other words you know from this main function I am going to go and pass the iterator to this first function who is going to go and enumerate from 19 to 2001 right what he will do is he will start looping but as soon as you know the number of years go about 2000 he is going to pass this complete iteration to this guy over here and he will take take from that so in other words now now if you see the output right the output is definitely very easy to understand so if you if I go and done this in a debug mode now so that is my numerator now force this enumerator goes to the first function he starts moving displays the first value so the first value is 1990 right and he looked you know until sorry for that he loots sorry so he looks you know until number of years ago about 2,000 so you can see now it is 2001 so he is now entering the other function so he's passing this iterator or this nm rater I'll say rather in a rule other function so he can I can see that he has now started looking from here and from here this enumerator we can see now is taking the values ahead of 2000 they know ahead of 2001 you know that is 2002 and 2003 so we can see that the second function is now taking 2002 and then he's taking 2003 right so if I see the final output from 1990 to 2001 it was in a loop by using iterator iterate 92 2001 function while the iterate 2001 and of all function actually helped me to loop 2002 and 2003 in other words you know when I was passing this innovator from one function to other function he was knowing his current cursor position he was knowing you know what exactly is his current cursor position and from where to start alright now let us try to do this why with ienumerable so what I'm going to do is I'm going to make this as I enumerable write this again as I enumerable right and what I'll do is I'll just you know for inma will we all know that we have to use a for each loop some to say for each you know int I in Oh so if Oh is greater than 2000 and just passed this complete enumeration to the other function so this o is integer sorry the I so if the I is greater than 2000 the current value is greater than 2000 just move this what you call I enumerable to the next guy that is this function it written iteration it late 2001 and above and over here also I am going to just go and control see this control V and Here I am going to go and loop and display the values so say console dot write line I and we'd also like to display this values over here also some basic control right now what we're trying to do is I'm going to go and comment this code I'm going to uncomment this code here and I'm going to say yes pass this I enumerable to iterate 19 2001 and about write 19 2019 to 2001 I'm sorry my my function names are very confusing at this moment I'm sorry for that right so what we're doing now is the same thing what we did with ienumerator now we are trying to you know you you know replace it with inm variables so you can see that I have created the I in my collection here and I'm passing it to you know this function first he loops it the time the value goes above 2000 right he actually passes this I enabled to the other function okay so I'm going to put a debug point here and let me do Fi so that it goes so it goes here n so it starts right so what the first value is 1995 not greater than 2000 goes ahead goes ahead and now the value is greater than 2000 so he goes here right so now you can see that he has looped take 2000 in other words he has looped this in over the first four or five values right so what should happen is actually a theoretically speaking is when he goes here to loop right you should start from 2001 and above right but if you go and see the value now of I you can see it is 1990 in other words ienumerable does not remember his cursive state he doesn't remember you know you know at the at which current position he belongs to so the biggest difference between ienumerable and ienumerator is that you know ienumerable remember states while i an invariable does not okay so you can see now over here you know this is again starting from completely from one so if you see the final output it is going to be weird right it's going to probably loop multiple times you can see and that's something wrong is alright completely why that has happened is because i anyway will does not remember you know till which cursor position he has loop - so let's sum up so if your requirement is that you want to just loop sequence you through the collection one by one and you you are not interested you know in where your cursor is currently you just want to loop through them then ienumerable is the best fit why because the code is shorter it is sweeter the syntax is much smaller as compared to the iterator or the enumerator I'll say now the ienumerator is it is a good fit you know where you you want to pass that in emulator you know from one function to other function and you want to remember to you know at what position the current cursor is then ienumerator is a better fit as compared to ienumerable so i hope that you enjoyed this video in this video we were trying to understand in what situation is I enumerable good and in what situation is I enumerator good thank you so much
Info
Channel: .NET Interview Preparation videos
Views: 443,075
Rating: 4.7643576 out of 5
Keywords: c#, .NET, IEnumerable, IEnumerator, questpond, Enumerable Interface, C# programming language, IEnumerable vs IEnumerator, c# for beginners, csharp interview questions, csharp interview questions and answers, difference between IEnumerable and IEnumerator, c# visual studio, visual studio c# code, ienumerable vs ienumerator, implementing ienumerable and ienumerator, use inumerable, use ienumerator
Id: jd3yUjGc9M0
Channel Id: undefined
Length: 13min 39sec (819 seconds)
Published: Sat Mar 31 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.