Question about Collection generic class

G

Guest

Here is the definition for System.Collection.ObjectModel.Collection generic
class:
[SerializableAttribute]
[ComVisibleAttribute(false)]
public class Collection<T> : IList<T>, ICollection<T>,
IEnumerable<T>, IList, ICollection, IEnumerable
The class does not support IEnumerator or IEnumerator<T> interfaces. How
come I can still use "foreach" against the class? I think a class has to
support IEnumerable and IEnumerator for "foreach". Anyone can tell me the
reason? I need to implement a class similar to it (can not derive from it in
my case).
 
A

Alex Meleta

Hi Roy,

Because ICollection already has IEnumerable, needed for a foreach loop to
access the enumerator. IList based on ICollection. So here is more then enough
enumerators.

Regards, Alex
[TechBlog] http://devkids.blogspot.co
 
A

Alex Meleta

Hi Roy,

Yes, but foreach uses IEnumerable to obtain IEnumerator (IEnumerable.GetEnumerator()).
Without IEnumerable only while (or such) can be used to "manualy" iterating
the collection, not the foreach statement.

Regards, Alex
[TechBlog] http://devkids.blogspot.com

R> Is IEnumerator needed for "foreach"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top