Alex Rivera | Logout

IEnumerable<T> provides two GetEnumerator methods - what is the difference between them?

Asked 2009-02-18T11:27:18.063
11

When I emplement IEnumerable<T> interface I see two GetEnumerator methods: one returning IEnumerator and other IEnumerator<T>. When would I use one or another?

Edit
Report

1 Answer

8

If you are implementing the IEnumerable<T> generic interface, you will pretty much always have to use the generic GetEnumerator method - unless you cast your object explicitly to (non-generic) IEnumerable.

The reason is backwards compatability with .NET 1.0/1.1 which didn't support generics.

answered 2009-02-18T11:33:48.360

Your Answer