Geeks With Blogs
Saqib Ullah BootStrapper Know How

The good question that first come in our mind when use class and when use interface.
"Classes are only appropriate for “is-a” relationships (where the derived class is really an instance of the base class), and interfaces are appropriate for all others.”.

foreach (Person objper in PersonCollection)
        Console.WriteLine(objper);

All collections implement the IEnumerable. IEnumerable is place on the collection object and it exposes the enumerator, which supports a simple iteration over a collection. With foreach loop statement we iterator all objects in collection. it returns a object that has implements the IEnumerator interface

IEnumerator custEnum = new PersonCollection.GetEnumertor();

On the other hand IEnumerator implement a property Current and two methods MoveNext() and Reset(). IEnumerator is base interface for all nongeneric enumerators. Enumerator cant modify data in the underlying collection it only read data in collection. Use current property without calling MoveNext method throw an execption, so you must call MoveNext method in advance before getting value into the current property.  The Reset method also brings the enumerator back to this position.
You can implement IEnumerable and IEnumerator on your custom objects.



 

Posted on Friday, December 15, 2006 2:20 PM .Net | Back to top


Comments on this post: IEnumerable and IEnumerator

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Saqib Ullah | Powered by: GeeksWithBlogs.net