Click here to Skip to main content
15,887,262 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I was reading about IEnumerator from CodeProject but I was unable to find out what is the actual use of IEnumerator.
Posted
Updated 5-Jan-11 21:52pm
v2
Comments
Dalek Dave 6-Jan-11 3:52am    
Minor Edit for Grammar.

It's just for creating functionality like foreach in your class.

you will find THIS[^] answer convincing.
 
Share this answer
 
v2
Many interfaces and classes inherit/implements the IEnumerable<T> interface to implement enumerators. IEnumerable declares a method

public IEnumerator GetEnumerator()

and

IEnumerator supports a simple iteration over collections

See
http://msdn.microsoft.com/en-us/library/78dfe2yb.aspx[^]

and

http://msdn.microsoft.com/en-us/library/s793z9y2.aspx[^]

Regards
Espen Harlinn
 
Share this answer
 
Comments
Dalek Dave 6-Jan-11 3:52am    
Good Links.
Manfred Rudolf Bihy 6-Jan-11 15:16pm    
Agreed! 5+
Espen Harlinn 6-Jan-11 15:25pm    
Thanks Manfred!
This[^] might help you out as well.
 
Share this answer
 
Come on, the usual use of IEnumerable is foreach contruct; start from there:

C#
var items = new SomeClass(/* ... */);
//...

foreach(var item in items) { /* use item */ }


How can it be possible? This is because SomeClass
implements IEnumerable:

C#
class SomeClass : IEnumerable {
    IEnumerator IEnumerable.GetEnumerator() { /* ... */ }
    //...
} //class SomeClass


As soon as you get it, read about using and IEnumerable. It will show you need to implement GetEnumerator and return IEnumerator. And finally IEnumerator requires you to implement iteration steps -- easy enough.
 
Share this answer
 
v6
Comments
fjdiewornncalwe 6-Jan-11 14:57pm    
My choice for the best answer, but the only one not marked as the solution.
Sergey Alexandrovich Kryukov 6-Jan-11 21:42pm    
Thanks a lot, Marcus.
Manfred Rudolf Bihy 7-Jan-11 19:24pm    
You have my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900