0

Arrays are non-generic. They implement IEnumerable but not IEnumerable<T>. So how is it possible that the following code compiles and executes fine ?

int[] myArray = {1,2,3,4};
IEnumerable<int> myEnum = myArray;
List<int> myList = new List<int>(myArray); 
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
sam95
  • 99
  • 2
  • 5
  • 4
    Arrays *do* implement `IEnumerable`, see the linked duplicate question for details. – Heinzi Nov 04 '15 at 10:29
  • [Why do arrays in .net only implement IEnumerable and not IEnumerable?](http://stackoverflow.com/questions/2773740/why-do-arrays-in-net-only-implement-ienumerable-and-not-ienumerablet) – Tim Schmelter Nov 04 '15 at 10:29
  • 1
    @Heinzi: Well, some do. Not all. `int[,]` doesn't implement `IEnumerable` for example... – Jon Skeet Nov 04 '15 at 10:34
  • 1
    SZArrays (single-rank arrays with 0 as the lower bound) implement `IEnumerable` (and `IList`), but they do so in a different way to how classes and structs implement arrays, so some of the things we'd expect to see when an interface is implemented, we don't. `typeof(int[]).GetInterfaceMap(typeof(IEnumerable))` for example throws an exception. http://stackoverflow.com/a/31883327/400547 explains how they are implemented and why these differences happen. – Jon Hanna Nov 04 '15 at 10:42
  • @Heinzi, yes it is a duplicate, my bad, i hadnt seen those previous posts. Thanks – sam95 Nov 04 '15 at 11:00

0 Answers0