铸造阵列的IEnumerable< T> [英] Casting array to IEnumerable<T>

查看:130
本文介绍了铸造阵列的IEnumerable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个基本的员工类,例如:

Assume you have a basic Employee class as such:

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

然后(在一个单独的类),我有以下code片段(我想我明白所有,但最后一个):

Then (in a seperate class) I have the following code fragments (I think i understand all but the last):

我认为有以下code片段工作,因为该数组initiliser创建它们是同一类型的劳动力变量被分配到Employee对象的数组。

I believe that the following code fragment works because the array initiliser creates an array of Employee objects which are the same type as the workforce variable being assigned to.

Employee[] workforceOne = new Employee[] {
   new Employee() { Name = "David", Years = 0, Department = "software" },
   new Employee() { Name = "Dexter", Years = 3, Department = "software" },
   new Employee() { Name = "Paul", Years = 4, Department = "software" } };

然后我有以下的code片段。我相信这个工程,因为员工的阵列对象含蓄是数组()类,它实现的IEnumerable 的实现。因此,我相信这就是为什么阵列可以被分配到的IEnumerable

I then have the following code fragment. I believe this works because the array of Employee objects implicity is an implementation of the Array() class which implements IEnumerable. Therefore, I believe this is why the array can be assigned to IEnumerable?

IEnumerable workforceTwo = new Employee[] {
   new Employee() { Name = "David", Years = 0, Department = "software" },
   new Employee() { Name = "Dexter", Years = 3, Department = "software" },
   new Employee() { Name = "Paul", Years = 4, Department = "software" } };

然后我有这个code片段:

Then I have this code fragment:

IEnumerable<Employee> workforceThree = new Employee[] {
   new Employee() { Name = "David", Years = 0, Department = "software" },
   new Employee() { Name = "Dexter", Years = 3, Department = "software" },
   new Employee() { Name = "Paul", Years = 4, Department = "software" } };

我不知道为什么,这code片段作品 的IEnumerable&LT;员工&GT; 继承了IEnumerable (和覆盖(或过载)在的GetEnumerator()方法?),但不应该因此,我需要上面铸这样的工作:

I am not sure why this code fragment works? IEnumerable<Employee> inherits from IEnumerable (and overrides (or overloads?) the GetEnumerator() method) but shouldn't i therefore need a cast for the above to work as such:

//The cast does work but is not required
IEnumerable<Employee> workforceFour = (IEnumerable<Employee>)new Employee[] {
   new Employee() { Name = "David", Years = 0, Department = "software" },
   new Employee() { Name = "Dexter", Years = 3, Department = "software" },
   new Employee() { Name = "Paul", Years = 4, Department = "software" } };

似乎阵列隐正在下降从类型的IEnumerable 的强制转换为的IEnumerable&LT;员工&GT; ,但当你需要一个类型转换为更具体的,你需要显式转换的东西我一直以为。

It seems that the array is being implicitly down cast from a type of IEnumerable to IEnumerable<Employee> but I always thought when you needed to convert a type to something more specific you needed an explicit cast.

也许我失去了一些东西简单的在我的理解在这里,但有人可以请帮我解决这个理解。

Maybe i'm missing something simple in my understanding here but can someone please help me with my understanding around this.

感谢您。

推荐答案

文档

在.NET Framework 2.0版,Array类实现了 System.Collections.Generic.IList&LT; T&GT; System.Collections中。 Generic.ICollection&LT; T&GT; System.Collections.Generic.IEnumerable&LT; T&GT; 通用接口。的实现提供给数组在运行时,因此不能对文档构建工具可见。因此,通用的接口不会出现在Array类的声明语法,并有接口成员只能通过投阵列的通用接口类型(显式接口实现)访问没有提及的话题。

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).

这样,你的员工[] 工具的IEnumerable&LT;员工方式&gt;

这篇关于铸造阵列的IEnumerable&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆