将数组转换为 IEnumerable<T> [英] Casting array to IEnumerable&lt;T&gt;

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

问题描述

假设你有一个基本的 Employee 类:

Assume you have a basic Employee class as such:

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

然后(在一个单独的课程中)我有以下代码片段(我想我理解除了最后一个):

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

我相信以下代码片段有效,因为数组初始化程序创建了一个 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" } };

然后我有以下代码片段.我相信这是有效的,因为 Employee 对象的数组隐式是实现 IEnumerable 的 Array() 类的实现.因此,我相信这就是为什么数组可以赋值给 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" } };

然后我有这个代码片段:

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" } };

我不知道为什么这个代码片段有效? IEnumerable 继承自 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 但我一直想什么时候需要将类型转换为某种类型更具体地说,你需要一个明确的演员.

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.IListSystem.Collections.Generic.ICollectionSystem.Collections.Generic.IEnumerable 通用接口.实现在运行时提供给数组,因此对文档构建工具不可见.因此,泛型接口不会出现在 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).

因此,您的 Employee[] 实现了 IEnumerable.

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

这篇关于将数组转换为 IEnumerable<T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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