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

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 พ.ย. 2024

ความคิดเห็น • 253

  • @dnfvideo
    @dnfvideo  3 ปีที่แล้ว

    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

  • @ZhuShisubi
    @ZhuShisubi 10 ปีที่แล้ว +47

    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! :)

  • @kvguitarist
    @kvguitarist 11 ปีที่แล้ว +8

    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!

  • @muslimresearcher6062
    @muslimresearcher6062 6 ปีที่แล้ว +6

    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.

    • @shiftstudiong
      @shiftstudiong 3 ปีที่แล้ว

      thank you for this concept.

  • @khurramsultan2040
    @khurramsultan2040 7 ปีที่แล้ว

    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

  • @rajkumararora7366
    @rajkumararora7366 2 ปีที่แล้ว +4

    Very well explained in simplistic words.
    This truely signifies the role of a good teacher.
    Shiv sir you are the best !!! Keep it up...

  • @AhmadElkhouly
    @AhmadElkhouly 8 หลายเดือนก่อน

    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.

  • @kvguitarist
    @kvguitarist 11 ปีที่แล้ว +7

    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!

    • @MsRohit35
      @MsRohit35 4 ปีที่แล้ว

      Was going to comment the same

  • @kris4117
    @kris4117 3 ปีที่แล้ว

    Now that I understood what I need to.... I am all set for Nirvana - Guru ji with your blessings.

  • @tthamil
    @tthamil 9 ปีที่แล้ว

    I was very confused between IEnumerable and IEnumerator before the video, I had a very clear explanations from your videos in depth.Thank You.

  • @kopilkaiser8991
    @kopilkaiser8991 ปีที่แล้ว

    I had these exact questions and you have answered really well with simple and clear explanation. Thank you!

  • @RajMalhotra-hf3bv
    @RajMalhotra-hf3bv 4 ปีที่แล้ว +1

    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

  • @arun020391
    @arun020391 6 ปีที่แล้ว

    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.

  • @ssushovan
    @ssushovan 7 ปีที่แล้ว

    You are a life saver. Finally, I get the difference between these two now.

  • @Archak77
    @Archak77 2 ปีที่แล้ว

    Excellent explanation sir ...Thank you 🙏

  • @yaarkhakf
    @yaarkhakf 9 ปีที่แล้ว +155

    now i can die in peace

  • @rajendraprasad8814
    @rajendraprasad8814 12 ปีที่แล้ว

    The video was awesome...i was finding it difficult to understand the difference from many sites, this video was crisp and clear...kudos

  • @thisistruevine
    @thisistruevine 4 ปีที่แล้ว

    Thank your clear explation sir thumbs up from PH🇵🇭

  • @MuhammadYousaf-gd5rs
    @MuhammadYousaf-gd5rs 5 ปีที่แล้ว

    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.

  • @eladyehuda8827
    @eladyehuda8827 3 ปีที่แล้ว

    You are a true professional and an excellent explanation

  • @AmmarTheTrainer
    @AmmarTheTrainer 6 ปีที่แล้ว +3

    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 ?"

    • @monochromenight8943
      @monochromenight8943 6 ปีที่แล้ว +2

      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.

    • @pratikgajjar6183
      @pratikgajjar6183 6 ปีที่แล้ว

      @@monochromenight8943 that was really helpful, thanks !

  • @THWUFAILUZAMAN
    @THWUFAILUZAMAN 3 ปีที่แล้ว +1

    we can use foreach with oyears right? then why you convert oyears to IEnumerable explicity?

    • @mrityunjoysen10
      @mrityunjoysen10 3 ปีที่แล้ว

      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#.

  • @zuzuba3481
    @zuzuba3481 5 ปีที่แล้ว

    Well done. No words to appreciate. Long time confusion is cleared..

  • @vinaykrishnasharma5024
    @vinaykrishnasharma5024 9 ปีที่แล้ว +5

    Cleared a very important concept. Thanks a lot.

  • @sadaqat4728
    @sadaqat4728 3 ปีที่แล้ว

    great work man. hardworking and talented people like you deserve subscribers and appreciation rather than those loser tiktokers.

  • @arunntbe
    @arunntbe 12 ปีที่แล้ว +1

    Really nice video!! you have mentioned that IEnumeratble internally uses IEnumerator, so why it does not remember the current cursor position?

  • @sushmabaijv4158
    @sushmabaijv4158 10 ปีที่แล้ว +1

    Thank you very much now I got very clear idea about these two interfaces thanks once again

  • @HammadKhanYT
    @HammadKhanYT 3 ปีที่แล้ว +1

    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.

  • @sebastiankumlin9542
    @sebastiankumlin9542 5 ปีที่แล้ว

    Better than most other explanations!

  • @PVP-learn
    @PVP-learn 5 ปีที่แล้ว +1

    Great explanation ...u reduced my stresssss..

  • @jimmynguyen7961
    @jimmynguyen7961 6 ปีที่แล้ว +1

    Love these videos. Very comprehensive explanations.

  • @Nicetrycutiepie
    @Nicetrycutiepie 3 ปีที่แล้ว

    Best example I have seen,it's registered in my mind.

  • @phongchau1013
    @phongchau1013 8 ปีที่แล้ว

    Excellent explanation of the difference between the two. Thanks.

  • @mrk10000
    @mrk10000 4 ปีที่แล้ว

    You always give excellent examples and analogy, thanks a lot for sharing

  • @henryv4222
    @henryv4222 8 ปีที่แล้ว

    good entertaining explanations- I use it for looping through controls in forms apps but I didn't know what differences between the two were

  • @MansoorAman
    @MansoorAman 6 ปีที่แล้ว

    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.

  • @charlliemurphy8381
    @charlliemurphy8381 4 ปีที่แล้ว

    Very good explanation, thank you.

  • @aske1602
    @aske1602 10 ปีที่แล้ว

    Thanks I have been looking for a decent explanation of the difference between the two collections.

  • @mahalakshmip9618
    @mahalakshmip9618 6 ปีที่แล้ว

    Very good Explanation for Enumerator and Enumerable

  • @ASP4U
    @ASP4U 4 ปีที่แล้ว

    Please try to make video on MVC Layout. which should not refresh entire layout for each request

  • @thegr8raj1
    @thegr8raj1 6 ปีที่แล้ว

    Very nice explanation. Thanks for the video.

  • @Rahul999_Official
    @Rahul999_Official 2 ปีที่แล้ว

    Very well explained the details.

  • @sudhid8765
    @sudhid8765 3 ปีที่แล้ว

    Super shiv sir thanks for making my learning easy. I got one doubt to make the ienumerable remember cursor position can we use getenumerator()?

  • @idlevandal69
    @idlevandal69 7 ปีที่แล้ว

    Clear and concise explanation.. Awesome..

  • @jamesjackson414
    @jamesjackson414 11 ปีที่แล้ว

    Very good video. As always, well explained, easy to follow code and example. Thanks for sharing with the world.

  • @jitendravirkayade3466
    @jitendravirkayade3466 7 ปีที่แล้ว

    it really help me to understand basic difference, Thanks,

  • @gen_jain
    @gen_jain 2 ปีที่แล้ว

    Wow Sir Awesome ! Nicely explained loved it 😊😊

  • @MrJojo1089
    @MrJojo1089 9 ปีที่แล้ว +4

    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???

    • @Grygier94
      @Grygier94 7 ปีที่แล้ว +2

      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.

    • @MerinNakarmi
      @MerinNakarmi 6 ปีที่แล้ว

      By limiting the functionality, does it have any benefit, like in terms of time and memory?

  • @bhuwanpandey4342
    @bhuwanpandey4342 6 ปีที่แล้ว +3

    IEnumerable internally use IEnumerator then why it does not maintain the state

  • @viranirav
    @viranirav 11 ปีที่แล้ว +1

    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.

    • @Sunstriderko
      @Sunstriderko 2 ปีที่แล้ว

      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.

  • @paulvincent9473
    @paulvincent9473 3 ปีที่แล้ว

    Very good explanation

  • @bbabak2
    @bbabak2 4 ปีที่แล้ว

    Perfect explanation. Thank you

  • @bklynmarine91
    @bklynmarine91 10 ปีที่แล้ว +3

    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?

    • @VArsovski10
      @VArsovski10 10 ปีที่แล้ว +1

      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

  • @minhaj040182
    @minhaj040182 6 ปีที่แล้ว +6

    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.

    • @dnfvideo
      @dnfvideo  6 ปีที่แล้ว +3

      IEnumerator does not have a back method as such. So its not REALLY Bidirectional

  • @rajasekharjangam4999
    @rajasekharjangam4999 4 ปีที่แล้ว

    Super explanation bro..

  • @ISHANICUTIEPIE
    @ISHANICUTIEPIE 2 ปีที่แล้ว

    Superb explanation

  • @ashamp1028
    @ashamp1028 11 ปีที่แล้ว

    Very nice and clear explanation... Thanks a lot

  • @saranjeetkaur5184
    @saranjeetkaur5184 7 ปีที่แล้ว

    Thanks a lot Sir for explaining very well.

  • @SomethingWOW0
    @SomethingWOW0 ปีที่แล้ว

    this video explanation was boom boom bummer. Not a short and sweet. I am going to sleep, see you tomorrow .

  • @rtrs5413
    @rtrs5413 3 ปีที่แล้ว

    Please make a video on iqueryable and idisposable because you are best

  • @seanshimon
    @seanshimon 9 ปีที่แล้ว

    That example very good. Thanks.

  • @dnfvideo
    @dnfvideo  12 ปีที่แล้ว

    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.

  • @ajayjagdale2010
    @ajayjagdale2010 8 ปีที่แล้ว

    Very good explaination

  • @VijayKumar-fs1mm
    @VijayKumar-fs1mm 9 ปีที่แล้ว

    Very good explanation, Thank you.....

  • @abhinandan4444
    @abhinandan4444 4 ปีที่แล้ว

    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

  • @davidbarth80
    @davidbarth80 7 ปีที่แล้ว +1

    in practice where would you apply the technique to pass the enumator to another function?

    • @manchusandy1
      @manchusandy1 3 ปีที่แล้ว

      I'm also looking for the same bro. Please respond to this comment in case you found the answer.

  • @LOGIGON
    @LOGIGON 10 ปีที่แล้ว

    Thanks, you made the Difference clear!

  • @LoveShowsNostalgic
    @LoveShowsNostalgic 3 ปีที่แล้ว

    Thanks a lots keep posting for us please 🙏👍 ☺️

  • @mrsoo9687
    @mrsoo9687 8 ปีที่แล้ว +1

    i love your explanation! thank you so much! you saved me!

  • @malehakim248
    @malehakim248 11 ปีที่แล้ว +12

    I still dont understand why and when to use IEnumerable or IEnumerator. I see all you've done can be easily achieved with the List above....Please enlight me on this.

    • @anastasialaric1517
      @anastasialaric1517 7 ปีที่แล้ว +6

      For example you have a program in which you want to loop through some list but you want to restrict the collaborator from accessing methods from the List (Clear, Remove At etc). You will use IEnumerable to achieve this. Basically what collaborator can't do know is Clear list which ensures there will be no unexpected behaviour of the program.

    • @PramodSingh-kc1ne
      @PramodSingh-kc1ne 5 ปีที่แล้ว

      For example you have a program in which you want to loop through some list but you want to restrict the collaborator from accessing methods from the List (Clear, Remove At etc). You will use IEnumerable to achieve this. Basically what collaborator can't do know is Clear list which ensures there will be no unexpected behaviour of the program.

    • @rostfomin
      @rostfomin 4 ปีที่แล้ว +2

      @@PramodSingh-kc1ne Wtf you copy pasted the same comment from above a year later?

  • @kingwoodbudo
    @kingwoodbudo 5 ปีที่แล้ว

    Thanks for making this much clearer. I appreciate your time.

  • @pankajjha1914
    @pankajjha1914 4 ปีที่แล้ว

    thnku sir. it was very helpful.

  • @avinashsharma5942
    @avinashsharma5942 10 หลายเดือนก่อน

    Is there any difference between enumerator and yield coz they both perform stateful iteration

  • @tchipilev9
    @tchipilev9 7 ปีที่แล้ว

    Very good explanation. Thank you dude !

  • @nikolaypanchev
    @nikolaypanchev 9 ปีที่แล้ว

    Simple and clear explained. Thank You!

  • @P0mpal0
    @P0mpal0 6 ปีที่แล้ว

    !Very clear explanation, thanks

  • @highlightspoint6182
    @highlightspoint6182 8 ปีที่แล้ว

    excellent lecture clear view

  • @sravankumar131
    @sravankumar131 6 ปีที่แล้ว

    Nice explanation...Thanks alot sir...

  • @korlakuntanagendrababu2432
    @korlakuntanagendrababu2432 ปีที่แล้ว

    Nice explanation

  • @maheswarreddy.madithati
    @maheswarreddy.madithati 10 ปีที่แล้ว

    Very good video...........

  • @sahilchugh6482
    @sahilchugh6482 6 ปีที่แล้ว

    u r superb sir.. Keep helping us

  • @salimshaiks
    @salimshaiks 9 ปีที่แล้ว

    Really helpful video....Thanks a ton.

  • @sdss315
    @sdss315 12 ปีที่แล้ว +1

    This is nice, I learned it,
    Thank you sir !

  • @BrunoDemolishment
    @BrunoDemolishment 12 ปีที่แล้ว +1

    Wow, very good! Would you do a video on the ICollection interface, the IList interface and the IDictionary interface?
    You explaine very well peace

  • @phantomapprentice6749
    @phantomapprentice6749 2 ปีที่แล้ว

    Best teacher

  • @Bigblue_YT
    @Bigblue_YT 11 ปีที่แล้ว

    Tanks for this show. I learned a lot with video.

  • @dilshadansari8397
    @dilshadansari8397 9 ปีที่แล้ว

    Great Example..thanks alot..

  • @aspnet4456
    @aspnet4456 6 ปีที่แล้ว

    Good explanation. Thanks!!!

  • @manjeetnandal1
    @manjeetnandal1 5 ปีที่แล้ว

    Great sharing...

  • @UbergonMX
    @UbergonMX 4 ปีที่แล้ว

    wouldn't it be the same if somehow u change the method you do "yield return" on IEnumerable?
    seeing your "c# Yield keyword". which would be better?

  • @ManasTunga
    @ManasTunga 11 ปีที่แล้ว

    I think it is completely base lase to compare IEnumerable interface with the IEnumerator
    interface, and to conclude that the former doesn't remembers the cursor position and later does.

  • @Mckandy47
    @Mckandy47 7 ปีที่แล้ว

    Thalaivaaa. You are great !!!

  • @factworld4375
    @factworld4375 5 ปีที่แล้ว

    Thanks sir ji.
    Loved the way u have explained it.

  • @SushrutKanetkar
    @SushrutKanetkar 7 ปีที่แล้ว

    Thanks ! Very clear explanation.

  • @venkatasaisrikarganugapati7567
    @venkatasaisrikarganugapati7567 8 ปีที่แล้ว

    Decent Explanation.

  • @ChandanGupta-zp1sh
    @ChandanGupta-zp1sh 10 ปีที่แล้ว

    Wonderful explanation , very informative
    Thank you for sharing!

  • @brynarn
    @brynarn 5 ปีที่แล้ว

    Love your videos

  • @tornadoofthoughts
    @tornadoofthoughts 7 ปีที่แล้ว

    Great explanation!

  • @parajn
    @parajn 7 ปีที่แล้ว

    use for loop with index to find position when using ienurable

  • @vexedev
    @vexedev 11 ปีที่แล้ว +1

    Maaaaan, THIS IS A TUTORIAL!!! Amaaaxxxxing :D