Implementing IEnumerable

  Рет қаралды 10,725

Coding Tutorials

Coding Tutorials

4 жыл бұрын

Coding Tutorial: The C# foreach loop works on any standard collection, but what do we need to do to make it work on collections we've written for ourselves?
Source code available at: github.com/JasperKent/Impleme...

Пікірлер: 42
@CodingTutorialsAreGo
@CodingTutorialsAreGo 4 жыл бұрын
Anything else you want to know? Just ask. Download the source code at github.com/JasperKent/Implementing-IEnumerable Keep up to date by subscribing: kzbin.info/door/qWQzlUDdllnLmtgfSgYTCA
@theoceandragongaming
@theoceandragongaming 8 ай бұрын
That some in depth explanation. I never got that before.
@mrsajjad30
@mrsajjad30 Жыл бұрын
I highly admire your level of understanding with the topic and what a beautiful way to present all those concepts in a precise way. Thank you so much for making and sharing this gold with the community.
@williamturns1647
@williamturns1647 Жыл бұрын
Tne most complete explanation of the IEnumerable interface on the internet!
@MankritQuantum
@MankritQuantum 11 ай бұрын
brilliant video.Must see and try if Enumerable is giving you any issue
@johnyw1049
@johnyw1049 2 жыл бұрын
Mr Kent You are amazing . I never thought I could find such a video anywhere . TY
@oblivion3799
@oblivion3799 2 жыл бұрын
Best of video about ienum ... You cover all things about it ... Thank you so much
@andreivarga8440
@andreivarga8440 Жыл бұрын
Very good explanations, keep up the good work!
@lukasmodry196
@lukasmodry196 Жыл бұрын
Safed my day thnx mate.
@kmietek09
@kmietek09 3 жыл бұрын
Thank you very much sir, that was reaaallly helpful!
@ddr1706
@ddr1706 Жыл бұрын
Dope tutorial. Thanks man
@tosinmorufakinyemi6882
@tosinmorufakinyemi6882 3 жыл бұрын
good insight into the Ienumerable,. thank you sir
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
My pleasure.
@ThebroNight1
@ThebroNight1 3 жыл бұрын
I was expecting a KFC promotion.
@0i0l0o
@0i0l0o Жыл бұрын
Beautiful.
@paulofernandoee
@paulofernandoee 2 жыл бұрын
Great content, thanks!
@mistervoldemort7540
@mistervoldemort7540 Жыл бұрын
Thanks a lot
@leosilva0411
@leosilva0411 3 жыл бұрын
Nice video! Thank you!
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
Glad you liked it!
@olegsuprun7590
@olegsuprun7590 3 жыл бұрын
Great info, one question, is it possible to apply to enums? Or apply this pattern to a class with no backing collection, but be able to enumerate through its fields? For example i have a class with a lot of const string fields(100+), and i want to be able to enumerate them.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
There are ways to do both of those, but they are a bit tricky, so I'll do another video rather than try to explain here. If all goes to plan, I'll post it next Thursday.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
Actually, I realized that I've already done this for enums here: kzbin.info/www/bejne/jmqaZpV8f9uHnLs. The class of strings is a bit different, so I'll put together a video for that.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
The new video is up now: kzbin.info/www/bejne/oJzHeaR9h691i5Y
@dennisnaiden4747
@dennisnaiden4747 2 жыл бұрын
So using: " _somevariable.Count " does not iterate via IEnumerable under the hood ? If "_somevariable.Count " does not iterate fully in first place, why i couldnt set .Count as condition in a for-loop and be same memory efficient ? Im new to C#.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
Good question. For a collection which has a Count property, then writing a regular for-loop is not really any different in terms of functionality or efficiency from writing a foreach-loop. However, the foreach-loop syntax tends to be easier (once you're used to it). Plus, the foreach-loop works on any collection that implements IEnumerable, whereas the for-loop has the additional requirement of having a Count.
@phantastik3389
@phantastik3389 3 жыл бұрын
Wonderful!
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
Cheers! :)
@randomname6444
@randomname6444 3 ай бұрын
that seems like so much work for something so basic. having an extra class in there (bookenumerator) just for that is really unsatisfying. and then the historic non-generic version. i just want to provide the next element and eventually reset and it ends in all this boilerplate and clutter. is there no simplistic solution to this simple functionality?
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 ай бұрын
You hardly ever need to do this. In most cases you just expose the enumerator of the private collection. I've never written an enumerator for myself in commercial code.
@randomname6444
@randomname6444 3 ай бұрын
@@CodingTutorialsAreGolet me just say I'm amazed at your reply speed, thumbs up is well deserved, thank you for your work. I have a class with an array of lists at its center (my attempt at a custom hashmap). I implemented an indexer for the class and now my plan was to implement ienumerable. that would probably be one of those cases where i would have to write my own enumerator, no?
@prophy6680
@prophy6680 3 жыл бұрын
Can i just make the list public so i can acces it in the foreach loop ?
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
Good question. The problem with making the list public would be (depending on circumstances) that you give the client code *too* much access. They'd to able to add to it as well as merely read it. One alternative is to give them public access but only through the IReadonlyCollection interface, which does as the name suggests.
@alex-dk2rj
@alex-dk2rj 3 жыл бұрын
@@CodingTutorialsAreGobut to me accessing books by Libray[idx] doesn’t make sense. Realistically, a library could have many properties like name, location, hours, employees, etc.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
Fair point. I think regarding a library as a collection of books is a reasonable default, but you're right; it could equally be regarded as a collection of Employees. There are various ways round that, but my instinct would be to use interfaces, so class Library : IEnumerable, IEnumerable {...} and then use explicit interface implementation to distinguish the two. If you want to use and indexer, you'd have to find another interface that includes the indexer, but indexers are so rarely used, I doubt it's worth the bother. You get ElementAt() for free as an extension to IEnumerable.
@youtischia
@youtischia 2 жыл бұрын
Nice video. But why do we need Ienumerable at all ? All the work is done by Ienumerator. Ienumerable does not do anything other return Ienumerator.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
To an extent, you are right. foreach does not require the collection to implement IEnumerable, merely to have a method GetEnumerator which returns an IEnumerator. But more generally in C#, it is not enough for an object to have a method, it must declare in a standardized way that it has the method - and that's done through an interface. Thus IEnumerable declares that a class has GetEnumerator without the need for any more detailed checks.
@youtischia
@youtischia 2 жыл бұрын
@@CodingTutorialsAreGo Isnt this an unnecessary hoop to jump thru ? Since the object in your example uses a list, why doesnt the compiler make our lives easy and just realise that it already has a getenumerator ? Just because the list is in an oject shouldnt matter. The information is already there. If someone wanted to define a new one for their new container all well and good. Couldnt it all be "under the hood".
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
@@youtischia It could be done like that, but it's not the way languages like C# go. The problem would be that if you just happened to write a method called GetEnumerator that was nothing to do with this feature (unlikely in this case, but highly possible with interfaces in general) then the system would pick it up anyway. The idea is that the developer declares the behaviour they want, the compiler doesn't assume it.
@WaahaidIWKY
@WaahaidIWKY 3 жыл бұрын
well selling chicken isn't his only hobby
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 жыл бұрын
I'd never have imagined that Colonel Sanders would sound like me when he opens his mouth.
@stewiegriffin6503
@stewiegriffin6503 Жыл бұрын
you are very confusing, because u jumping from one thing to onother
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
Sorry.
C# Covariance
17:27
Coding Tutorials
Рет қаралды 7 М.
C# Equality and Hashcodes
27:05
Coding Tutorials
Рет қаралды 8 М.
Watermelon Cat?! 🙀 #cat #cute #kitten
00:56
Stocat
Рет қаралды 23 МЛН
Заметили?
00:11
Double Bubble
Рет қаралды 3,5 МЛН
When Steve And His Dog Don'T Give Away To Each Other 😂️
00:21
BigSchool
Рет қаралды 16 МЛН
Climbing to 18M Subscribers 🎉
00:32
Matt Larose
Рет қаралды 18 МЛН
C# Yield Return: What is it and how does it work?
15:09
Brian Lagunas
Рет қаралды 54 М.
The Dispose Pattern
16:28
Coding Tutorials
Рет қаралды 9 М.
C# 12 Collection Expressions and the Spread Operator
25:27
Coding Tutorials
Рет қаралды 1,6 М.
IEnumerable 🆚 IEnumerator Interfaces in C#
34:06
tutorialsEU
Рет қаралды 27 М.
C# - IEnumerator - 76
14:04
RDragon
Рет қаралды 1,2 М.
Delegates in C# - A practical demonstration, including Action and Func
1:09:11
C# Value Types and Reference Types
32:49
Coding Tutorials
Рет қаралды 4,6 М.
How charged your battery?
0:14
V.A. show / Магика
Рет қаралды 4,9 МЛН
cool watercooled mobile phone radiator #tech #cooler #ytfeed
0:14
Stark Edition
Рет қаралды 9 МЛН
#miniphone
0:16
Miniphone
Рет қаралды 2,8 МЛН
📦Он вам не медведь! Обзор FlyingBear S1
18:26
Samsung S24 Ultra professional shooting kit #shorts
0:12
Photographer Army
Рет қаралды 12 МЛН