52

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

7
  • 4
    Why would you implement your own ArrayList class when there are already over a dozen existing collection classes? Commented May 5, 2010 at 13:49
  • 105
    Why wouldn't I? I'm tired of those kinds of comments. Commented May 5, 2010 at 13:53
  • 6
    We don't ask these kind of questions to annoy you, but to find out what it is you are trying to do. This helps us supplying you with a better answer. Commented May 5, 2010 at 14:08
  • 5
    Yes, I am implementing an ArrayList because although all languages have some sort of ArrayList provided in its framework, I assumed .NET didn't have one. Thanks guys for clearing it up. Commented May 5, 2010 at 14:11
  • 26
    These kind of questions are annoying though, because they seek to divert the conversation and the answers away from what is being asked. I always find it's better to answer what was asked no matter how silly it "seems". Commented Sep 19, 2017 at 16:43

2 Answers 2

71

Arrays do implement IEnumerable<T>, but it is done as part of the special knowledge the CLI has for arrays. This works as if it were an explicit implementation (but isn't: it is done at runtime). Many tools will not show this implementation, this is described in the Remarks section of the Array class overview.

You could add a cast:

return ((IEnumerable<T>)_array).GetEnumerator();

Note, older MSDN (pre learn.microsoft.com) coverage of this changed a few times with different .NET versions, check for the remarks section.

Sign up to request clarification or add additional context in comments.

3 Comments

It works just as if the "class" T[] implemented IEnumerable<T> by an explicit interface implementation. If you simply say _array.GetEnumerator() you get instead the public method which has the wrong return type.
@RayL See the .NET 2.0 version of the documentation :-) updating.
Just a heads up. From the referenced msdn.microsoft.com/en-us/library/system.array.aspx#Remarks: it implements IList<T>, ICollection<T> and IEnumerable<T> generic interfaces.
-2

You can use generic method IEnumerable<T> OfType<T>() from System.Linq namespace, which extends IEnumerable interface. It will filter out all elements which type is different than T and return IEnumerable<T> collection. If you use (IEnumerable<T>)_array conversion operator, it might not be safe, because System.Array (and other nongeneric types) stores items of type System.Object.

1 Comment

Only arrays of Objects (i.e. Object[] arr) stores the item as an object. Other than those, array's are type safe. That makes the OfType and Cast methods redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.