Casting array to IEnumerable<T> Casting array to IEnumerable<T> arrays arrays

Casting array to IEnumerable<T>


From the documentation:

In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic.IList<T>, System.Collections.Generic.ICollection<T>, and System.Collections.Generic.IEnumerable<T> generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear 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 to the generic interface type (explicit interface implementations).

Thus, your Employee[] implements IEnumerable<Employee>.


The Array of Employees by default implements IEnumerable<Employee> as well as IEnumerable


Explicit cast is needed when some sentence needs to be downcasted. That's casting an object to a more specialized type - if the object is of such specialized type -.

In the other hand, upcasting (casting to a less specialized type), will never need an explicit cast, but you can explicitly do it (it's just useless).

Since Array implements IEnumerable and IEnumerable<T>, you're doing an upcast in your code, meaning _you don't need to explicitly cast to IEnumerable<T>.