.NET Why can string [] be passed as an IEnumerable <string> parameter?
from: msdn array class
In the .NET Framework version 2.0, the Array class implements
IList<T>,ICollection<T>andIEnumerable<T>common interfaces. Implementations are provided to arrays at runtime and therefore are not visible to documentation assembly tools. As a result, common interfaces are not displayed in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array into a common interface type (explicit interface implementations). The key value to consider when creating an array on one of these interfaces is the elements that add, insert, or delete throw
NotSupportedExceptionelements.
how does the implicit conversion string [] → IEnumerable work?
This kind of misses item because there is no conversion. Implementation arrays (you could say almost “inherit from” if that makes sense to you) are IEnumerable, and so the string [] is already IEnumerable - there’s nothing to convert
This is a standard interface conversion; as a requirement, arrays ( T[] ) implement IEnumerable and IEnumerable<T> . Thus, string[] is 100% compatible with IEnumerable<T> . Implementation is provided by the compiler (arrays were mostly generic before .NET files were distributed).
From the specification (ECMA 334 v4) - this is a consequence of 13.1.4:
- From the one-dimensional array
S[]toSystem.Collections.Generic.IList<S>and the base interfaces of this interface. - From the one-dimensional array
S[]toSystem.Collections.Generic.IList<T>and the base interfaces of this interface, provided that there is an implicit reference conversion fromStoT
And remember that IList<T> : IEnumerable<T>