views:

108

answers:

1

I was implementing my own ArrayList class and was left surprised when I realised that

public System.Collections.Generic.IEnumerator<T> GetEnumerator() {
    return _array.GetEnumerator();
}

didn't work. What is the reason arrays don't implement IEnumerator in .NET?

Is there any work-around?

Thanks

+10  A: 

Arrays do implement IEnumerable<T>, but it is done as part of the special knowledge the CLI has for arrays. Many tools will not show this implementation. See the "Important" note in the documentation.

You could add a cast:

return ((IEnumerable<T>)_array).GetEnumerator();
Richard
Ah! That was it! Thanks
devoured elysium