0

Each C# array is supposed to implicilty derive from class Array which implements the non-generic IEnumerable

 public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable

IEnumerable gets only four extension methods. However, each array gets also extension methods from the generic IEnumerable<<T>>. The problem is that class Array does not implement IEnumerable<<T>> The MSDN docs for class Array state that "Starting with the .NET Framework 2.0, the Array class implements the generic interfaces like System.Collections.Generic.IEnumerable<<T>> The implementations are provided to arrays at run time, and as a result, the generic interfaces do not appear in the declaration syntax for the Array class"

So my question is why the class Array does not implement IEnumerable<<T>> explicitly when it implements IEnumerable<<T>> implicitly anyway. What does it mean that implemenation is provided at runtime when the compiler compiles it at compile time, and Intellisense shows extension methods for IEnumerable<<T>> added to any array variable at edit time.

Micky6789
  • 43
  • 5
  • As to the "how does it provide the generic version at runtime": https://stackoverflow.com/questions/11163297/how-do-arrays-in-c-sharp-partially-implement-ilistt – Tim Schmelter Jun 20 '17 at 15:05
  • This question already provided incomplete answers from similar questions in the past. The answers in the past merely quoted the documentation which fails to provide a logical justification – Micky6789 Jun 20 '17 at 15:06
  • @Micky6789 Arrays are special, and use code from the CLR that you couldn't write yourself in C#. Eric Lippert has a little extra info about this. He doesn't go in to detail, but explains it at a high level: "... things would have been a lot different if we'd had generics in CLR v1. Arrays are essentially a generic collection type. Because they were created in a type system that did not have generics, there has to be lots of special code in the type system to handle them." https://stackoverflow.com/a/5110960/310988 –  Jun 20 '17 at 15:34

0 Answers0