Do not miss our Interview Question video series 30 Important C# Interview Questions : th-cam.com/video/BKynEBPqiIM/w-d-xo.html 25 Important ASP.NET Interview Questions : th-cam.com/video/pXmMdmJUC0g/w-d-xo.html 25 Angular Interview Questions : th-cam.com/video/-jeoyDJDsSM/w-d-xo.html 5 MSBI Interview Questions : th-cam.com/video/5E815aXAwYQ/w-d-xo.html
I think the more accurate syntax for the difference between IEnumerable and IEnumerator is that: it is not the IEnumerable or IEnumerator maintains the state or not, but the technique of iteration through IEnumerable or IEnumerator rests the position or not. As you loop through IEnumerable with foreach loop, the foreach loop automatically resets the IEnumerable position to the initial position each time you iterate. While as you iterate through IEnumerator with MoveNext() method the position is maintained with the new iteration loop, because nothing automatically changes the position of IEnumerator and if you want the initial position of IEnumerator you must use Reset() method. So, the technique used to iterate through IEnumerable rests the position automatically each time you iterate, while The technique used to iterate through IEnumerator begins the iteration from the last IEnumerator position and no automatic reset is carried out in IEnumerator iteration.
Also at 10: 16 we should pass int i in the IEnumerable example to 2nd function Iterate2001andAbove() instead of passing o (which is the full list). This will fix listing the years multiple times... :-) Hope that helps.. Thank you once again!
i dont know why some stupid people are disliking these videos. this guy has great way of teaching and he is doing it for free and on the other hand he is very well experienced and sharing his experience
Great video... Would just like to point out one thing... Maybe no one noticed it but at 12:25, you say "The Biggest difference between IEnumerable and IEnumerator is that IEnumerable REMEMBERS STATES while IEnumerable DOES NOT." I think you wanted to say "IEnumerator remembers states while IEnumerable does not" Obviously that was an accident. But just wanted to point out. Thank you so much for the help!
Explained very good. Only one small correctionwhere you have said IEnumerable instead of IEnumerator. Statement from Video- " IEnumrable remember state while IEnumerable does not"Look at time 12:20 in video
Thank you. In short, IEnumerable is the collection itself that can be iterated. IEnumerator is an enumerator object that's used to iterate through a collection of data. So for a collection to be enumerable it must provide an enumerator object. IEnumerable exposes the GetEnumerator method that provides that enumerator.
There is a bug in this code though not obvious in the tutorial. @8:43 In the first function Upto2000, it checks for value weather greater than 200 and then jumps to the second function. But when it jump into the second function o.MoveNext is called and then it print the NEXT value not the current value. So the program is flawed in a sense that the condition check on a value, jump to the second function and then does not uses that value but pull in the next value. It will be more obvious if lets say after 2002, there is a value 1999, it will print in correct value.
Its already uploaded. we would like to clarify to everyone here what videos we upload on youtube are a very very small part what we have on questpondvd.
It's not about int, it's about IEnumerable. Because List implements more interfaces, not only IEnumerable and generally has more features. So when you store the reference of list in IEnumerable you won't have access to all List features, just the features IEnumerable implements. You got to cast it explicitly because you limit it functionality.
If foreach is the case and we're not interested in current position then why should we choose IEnumerable too instead of we might have use object of List in foreach right and why we go for IEnumerable? can you please anyone clear my doubt, Thank you.
at the end we get to know about the main difference between both IEnumerable and IEnumerator . But the question is " why we need all these to implement a loop ?"
This helped me understand the reasoning: programmingwithmosh.com/csharp/ienumerable-and-ienumerator/ To be able to use loops, the information needs to be exposed, which violates the OOP guideline of information hiding. Implementing the interfaces makes iteration possible without having to make the class's structure public.
Thanks for the video showing the difference between IEnumerable vs IEnumerator. Question, shouldn't we pass int i in the IEnumerable example (when modified at min:11) to 2nd function Iterate2001andAbove instead of passing o (which is the full list). This should fix the results & each year will get listed only once.
yes that would fix the problem. Anyways, the goal is to show that there might be some cases where you want to pass a whole collection to the next method and do something with it which is more complex than just writing it into console.
IEnumerator is built for creating your own data types which can be looped through. Its up to you how you implement it. You have 1 billion items in a file, if you create a IEnumerable data type to get these items you will be out of memory. But if you implement IEnumerator you can get 1 item at a time and only need 1 item memory to store that value. Then you move next and get next item. It will be very fast and instant for your application to load and loop items.
Total Out of Context. IEnumerable is only forward loop and commonly used to list any thing without user interfere while IEnumerator is bidirectional that is Forward and Backward and commonly used in paging view so that user can move forward or backward or reset while can not achieve by iEnumerable.
Great video, I think your conclusion may be misleading. It's not that the IEnumerable variable is unable to remember its state, it is that you iterate your IEnumerator variable through the data structure using the MoveNext method so that it points at the next element in each iteration through the for loop. The foreach loop used with the IEnumerable is clever enough to understand what you really want to do when you "loop through an IEnumerable"; because it loops through the data structure that the IEnumerable is currently pointing to, start at its current position.
I don't seem to get the point, why cast oyears to IEnumerable and still use a for each loop to iterate through when you could have used the list and done the same thing?
You never had the need to find Nth element in a list ??, or even better - on what position is the element X in a list ?? Funny how I always used a foreach, and then used an outside counter to "update" the current position, in the foreach-loop itself till a certain condition is met :P
when it was 2001 in the first method, then the list is passed to next method and in next method, the current value is 2001 but after while(o.movenext()) the current changes into 2002 so 2001 got kind of skip. right?
Thanks for the explanation however, IEnumerator seems similar to Yield since both maintain state iteration state. What could be the possible difference between them and in what scenarios we should specifically use them? Thanks in advance
If the o.Current was 2001 when it got passed to the other method, wouldn't the o.MoveNext in the While loop immediately move to 2002? Hence skipping 2001?
yes when it was 2001 in the first method, then the list is passed to next method and in next method, the current value is 2001 but after while(o.movenext()) the current changes into 2002 so 2001 got kind of skip.
Do not miss our Interview Question video series
30 Important C# Interview Questions : th-cam.com/video/BKynEBPqiIM/w-d-xo.html
25 Important ASP.NET Interview Questions : th-cam.com/video/pXmMdmJUC0g/w-d-xo.html
25 Angular Interview Questions : th-cam.com/video/-jeoyDJDsSM/w-d-xo.html
5 MSBI Interview Questions : th-cam.com/video/5E815aXAwYQ/w-d-xo.html
This question has been going around for years and finally someone who just answered it in a very in-depth yet simple way. Thanks from Master Zhu! :)
Very well explained in simplistic words.
This truely signifies the role of a good teacher.
Shiv sir you are the best !!! Keep it up...
now i can die in peace
RIP in peace.
:D
This made me laugh so hard. But its so true. Programming can get so confusing at times
LOL :D
:p
I think the more accurate syntax for the difference between IEnumerable and IEnumerator is that: it is not the IEnumerable or IEnumerator maintains the state or not, but the technique of iteration through IEnumerable or IEnumerator rests the position or not.
As you loop through IEnumerable with foreach loop, the foreach loop automatically resets the IEnumerable position to the initial position each time you iterate.
While as you iterate through IEnumerator with MoveNext() method the position is maintained
with the new iteration loop, because nothing automatically changes the position of IEnumerator and if you want the initial position of IEnumerator you must use Reset() method.
So, the technique used to iterate through IEnumerable rests the position automatically
each time you iterate, while The technique used to iterate through IEnumerator begins the iteration from the last IEnumerator position and no automatic reset is carried out in IEnumerator iteration.
thank you for this concept.
Also at 10: 16 we should pass int i in the IEnumerable example to 2nd function Iterate2001andAbove() instead of passing o (which is the full list). This will fix listing the years multiple times... :-)
Hope that helps.. Thank you once again!
Was going to comment the same
i dont know why some stupid people are disliking these videos. this guy has great way of teaching and he is doing it for free and on the other hand he is very well experienced and sharing his experience
I was very confused between IEnumerable and IEnumerator before the video, I had a very clear explanations from your videos in depth.Thank You.
Great video... Would just like to point out one thing... Maybe no one noticed it but at 12:25, you say "The Biggest difference between IEnumerable and IEnumerator is that IEnumerable REMEMBERS STATES while IEnumerable DOES NOT."
I think you wanted to say "IEnumerator remembers states while IEnumerable does not" Obviously that was an accident. But just wanted to point out. Thank you so much for the help!
I had these exact questions and you have answered really well with simple and clear explanation. Thank you!
You are a life saver. Finally, I get the difference between these two now.
The video was awesome...i was finding it difficult to understand the difference from many sites, this video was crisp and clear...kudos
Well done. No words to appreciate. Long time confusion is cleared..
Explained very good. Only one small correctionwhere you have said IEnumerable instead of IEnumerator. Statement from Video- " IEnumrable remember state while IEnumerable does not"Look at time 12:20 in video
You are a true professional and an excellent explanation
Thank you. In short, IEnumerable is the collection itself that can be iterated.
IEnumerator is an enumerator object that's used to iterate through a collection of data.
So for a collection to be enumerable it must provide an enumerator object. IEnumerable exposes the GetEnumerator method that provides that enumerator.
Great explanation ...u reduced my stresssss..
Thank your clear explation sir thumbs up from PH🇵🇭
Love these videos. Very comprehensive explanations.
Now that I understood what I need to.... I am all set for Nirvana - Guru ji with your blessings.
great work man. hardworking and talented people like you deserve subscribers and appreciation rather than those loser tiktokers.
Excellent explanation sir ...Thank you 🙏
There is a bug in this code though not obvious in the tutorial.
@8:43 In the first function Upto2000, it checks for value weather greater than 200 and then jumps to the second function. But when it jump into the second function o.MoveNext is called and then it print the NEXT value not the current value. So the program is flawed in a sense that the condition check on a value, jump to the second function and then does not uses that value but pull in the next value. It will be more obvious if lets say after 2002, there is a value 1999, it will print in correct value.
Cleared a very important concept. Thanks a lot.
Better than most other explanations!
Wow Sir Awesome ! Nicely explained loved it 😊😊
Clear and concise explanation.. Awesome..
Its already uploaded. we would like to clarify to everyone here what videos we upload on youtube are a very very small part what we have on questpondvd.
Thank you very much now I got very clear idea about these two interfaces thanks once again
You always give excellent examples and analogy, thanks a lot for sharing
Very good video. As always, well explained, easy to follow code and example. Thanks for sharing with the world.
Best example I have seen,it's registered in my mind.
Thanks a lots keep posting for us please 🙏👍 ☺️
Excellent explanation of the difference between the two. Thanks.
Really nice video!! you have mentioned that IEnumeratble internally uses IEnumerator, so why it does not remember the current cursor position?
Very good Explanation for Enumerator and Enumerable
A nice one. am just bit confused why did you need an explicit casting while creating the IEnumerable?The list was anyway of int right???
It's not about int, it's about IEnumerable. Because List implements more interfaces, not only IEnumerable and generally has more features. So when you store the reference of list in IEnumerable you won't have access to all List features, just the features IEnumerable implements. You got to cast it explicitly because you limit it functionality.
By limiting the functionality, does it have any benefit, like in terms of time and memory?
If foreach is the case and we're not interested in current position then why should we choose IEnumerable too instead of we might have use object of List in foreach right and why we go for IEnumerable? can you please anyone clear my doubt, Thank you.
Thanks I have been looking for a decent explanation of the difference between the two collections.
Very well explained the details.
it really help me to understand basic difference, Thanks,
Very nice explanation. Thanks for the video.
Very good explanation, thank you.
Super explanation bro..
Thanks for making this much clearer. I appreciate your time.
Very nice and clear explanation... Thanks a lot
Simple and clear explained. Thank You!
i love your explanation! thank you so much! you saved me!
good entertaining explanations- I use it for looping through controls in forms apps but I didn't know what differences between the two were
Thalaivaaa. You are great !!!
Tanks for this show. I learned a lot with video.
u r superb sir.. Keep helping us
Perfect explanation. Thank you
we can use foreach with oyears right? then why you convert oyears to IEnumerable explicity?
Yes right, I too thought the same question. Although I think as the video is 9 years old maybe lists were not directly iterable in that version of C#.
Super shiv sir thanks for making my learning easy. I got one doubt to make the ienumerable remember cursor position can we use getenumerator()?
This is nice, I learned it,
Thank you sir !
Very good explanation. Thank you dude !
Very good explanation
Thanks, you made the Difference clear!
this video explanation was boom boom bummer. Not a short and sweet. I am going to sleep, see you tomorrow .
Superb explanation
Maaaaan, THIS IS A TUTORIAL!!! Amaaaxxxxing :D
at the end we get to know about the main difference between both IEnumerable and IEnumerator . But the question is " why we need all these to implement a loop ?"
This helped me understand the reasoning:
programmingwithmosh.com/csharp/ienumerable-and-ienumerator/
To be able to use loops, the information needs to be exposed, which violates the OOP guideline of information hiding. Implementing the interfaces makes iteration possible without having to make the class's structure public.
@@monochromenight8943 that was really helpful, thanks !
Thanks for the video showing the difference between IEnumerable vs IEnumerator. Question, shouldn't we pass int i in the IEnumerable example (when modified at min:11) to 2nd function Iterate2001andAbove instead of passing o (which is the full list). This should fix the results & each year will get listed only once.
yes that would fix the problem. Anyways, the goal is to show that there might be some cases where you want to pass a whole collection to the next method and do something with it which is more complex than just writing it into console.
Thanks a lot Sir for explaining very well.
IEnumerator is built for creating your own data types which can be looped through. Its up to you how you implement it.
You have 1 billion items in a file, if you create a IEnumerable data type to get these items you will be out of memory. But if you implement IEnumerator you can get 1 item at a time and only need 1 item memory to store that value. Then you move next and get next item. It will be very fast and instant for your application to load and loop items.
Total Out of Context.
IEnumerable is only forward loop and commonly used to list any thing without user interfere while IEnumerator is bidirectional that is Forward and Backward and commonly used in paging view so that user can move forward or backward or reset while can not achieve by iEnumerable.
IEnumerator does not have a back method as such. So its not REALLY Bidirectional
!Very clear explanation, thanks
Great sharing...
Great video, I think your conclusion may be misleading.
It's not that the IEnumerable variable is unable to remember its state, it is that you iterate your IEnumerator variable through the data structure using the MoveNext method so that it points at the next element in each iteration through the for loop. The foreach loop used with the IEnumerable is clever enough to understand what you really want to do when you "loop through an IEnumerable"; because it loops through the data structure that the IEnumerable is currently pointing to, start at its current position.
Very good explanation, Thank you.....
6:48 why you need to convert it ? isnt it already an int.
thnku sir. it was very helpful.
Thanks sir ji.
Loved the way u have explained it.
Very good explaination
Wow, very good! Would you do a video on the ICollection interface, the IList interface and the IDictionary interface?
You explaine very well peace
I don't seem to get the point, why cast oyears to IEnumerable and still use a for each loop to iterate through when you could have used the list and done the same thing?
You never had the need to find Nth element in a list ??, or even better - on what position is the element X in a list ??
Funny how I always used a foreach, and then used an outside counter to "update" the current position, in the foreach-loop itself till a certain condition is met :P
Nice explanation...Thanks alot sir...
in practice where would you apply the technique to pass the enumator to another function?
I'm also looking for the same bro. Please respond to this comment in case you found the answer.
Very good video...........
Really helpful video....Thanks a ton.
You cleared up a lot for me, thank you
Please try to make video on MVC Layout. which should not refresh entire layout for each request
Love your videos
Great Example..thanks alot..
That example very good. Thanks.
Great explanation!
Thanks ! Very clear explanation.
when it was 2001 in the first method, then the list is passed to next method and in next method, the current value is 2001 but after while(o.movenext()) the current changes into 2002 so 2001 got kind of skip.
right?
Yes... Got any sol?
@@tuhinmanna1837 nope sorry
Nice explanation
Good explanation. Thanks!!!
excellent lecture clear view
Thanks for the explanation however, IEnumerator seems similar to Yield since both maintain state iteration state.
What could be the possible difference between them and in what scenarios we should specifically use them?
Thanks in advance
Best teacher
Very good explaination, thank you ! Now I understand :D
Please make a video on iqueryable and idisposable because you are best
Is there any difference between enumerator and yield coz they both perform stateful iteration
Decent Explanation.
your second example made me clear
If the o.Current was 2001 when it got passed to the other method, wouldn't the o.MoveNext in the While loop immediately move to 2002? Hence skipping 2001?
I see now. I watched it again. It does skip it but the 2001 gets written before the if > 2000 check.
yes when it was 2001 in the first method, then the list is passed to next method and in next method, the current value is 2001 but after while(o.movenext()) the current changes into 2002 so 2001 got kind of skip.