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).
Alex Meleta - 18 Jul 2007 15:42 GMT
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
Roy - 18 Jul 2007 16:52 GMT
Is IEnumerator needed for "foreach"?
> Hi Roy,
>
[quoted text clipped - 4 lines]
> Regards, Alex
> [TechBlog] http://devkids.blogspot.com
Alex Meleta - 18 Jul 2007 19:27 GMT
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"?