Casting array to IEnumerable<T>
Solution 1:
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>
, andSystem.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>
.
Solution 2:
The Array of Employees by default implements IEnumerable<Employee>
as well as IEnumerable
Solution 3:
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>
.