51

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

devoured elysium
  • 101,373
  • 131
  • 340
  • 557
  • 4
    Why would you implement your own ArrayList class when there are already over a dozen existing collection classes? – Aaronaught May 05 '10 at 13:49
  • 96
    Why wouldn't I? I'm tired of those kinds of comments. – devoured elysium May 05 '10 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. – Steven May 05 '10 at 14:08
  • 4
    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. – devoured elysium May 05 '10 at 14:11
  • 4
    @devouredelysium, .NET has a `List` class, which is the equivalent of the Java `ArrayList`. I think it's generally better to re-use built-in types than create your own. – Sam Nov 25 '13 at 05:45
  • 18
    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". – Owl Sep 19 '17 at 16:43
  • I see reasonable justification on both sides of the argument. After reading the original question and before reading this subthread, I hypothesized that he may have been implementing it either for practice or maybe for an assignment. Asking the "why implement your own" question did lead to some clarification about C#'s List vs Java's ArrayList, though. – Ryan McNames May 06 '19 at 17:12

2 Answers2

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.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • It works just as if the "class" `T[]` implemented `IEnumerable` by an _explicit interface implementation_. If you simply say `_array.GetEnumerator()` you get instead the `public` method which has the wrong return type. – Jeppe Stig Nielsen Oct 29 '14 at 18:29
  • @RayL See the .NET 2.0 version of the documentation :-) updating. – Richard Mar 18 '15 at 19:33
  • Just a heads up. From the referenced https://msdn.microsoft.com/en-us/library/system.array.aspx#Remarks: it implements `IList`, `ICollection` and `IEnumerable` generic interfaces. – Wouter Jan 23 '18 at 11:58
-1

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.

alcohol is evil
  • 686
  • 12
  • 34
  • 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. – jmoreno Jun 01 '17 at 15:56