Casting array to IEnumerable

后端 未结 3 1369
傲寒
傲寒 2020-12-02 21:53

Assume you have a basic Employee class as such:

class Employee
{
   public string Name;
   public int Years;
   public string Department;
}
         


        
3条回答
  •  孤城傲影
    2020-12-02 22:35

    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, you're doing an upcast in your code, meaning _you don't need to explicitly cast to IEnumerable.

提交回复
热议问题