IEnumerable

bpayne111

Junior Contributor
Joined
Feb 28, 2003
Messages
325
Location
BFE
Anyone up for explaining IEnumerable and IEnumerator?
I'm familiar with Interfaces, and i slightly understand the purpose of these 2 in particular. The help files have left me pretty confused. A tiny snippit of code would be wonderful.

Thanks
 
The IEnumerable interface is something that a class implements when it wants to be enumerator. It forces the class to expose a GetEnumerator function, which will return a class implementing the IEnumerator interface.

Any class implementing that second interface must expose a property reflecting the current item in the enumeration, and a method for moving forward and moving to the start of the iteration.

You don't always see these interfaces, but when you use a For Each statement (foreach in C#) it is using them under the hood.
 
yes actually that is the part that i understood, i do not understand how i'm supposed to implement it so that it works correctly with a for each statement
in other words i understand the concept but not the actual coding of the interface itself
care to explain a lil more?

thanks
 
ok so i've done some more research at msdn so i'd like to know if i got this right...
if i create a class that implements IEnumerable and then define my own movenext method...When i use a For Each... Next statement it will work according to how i defined it in MoveNext?
so hypethetically i could actually make it only enumerate through 1 item of a list if i'd like?

hmmm interesting
let me know

ps divil... one of these days i'm gonna help you lol

thanks
 
Yeah, something like that. Truth be told I've never implemented an enumerator manually, all my collections inherit from CollectionBase so it hasn't been a concern.
 
Back
Top