Question about Collection generic class

  • Thread starter Thread starter Guest
  • Start date Start date
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).
 
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
 
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"?
 
Back
Top