什么接口做所有阵列在C#中实现?阵列、接口

2023-09-02 20:43:05 作者:鱼柒

作为一个新的.NET 3.5的程序员,我开始学习LINQ和我发现,我以前没有注意到的东西pretty的基础:

As a new .NET 3.5 programmer, I started to learn LINQ and I found something pretty basic that I haven't noticed before:

这本书宣称每个阵列工具的IEnumerable< T> (很明显,否则根本无法使用LINQ对象的数组...)。当我看到这个,我心想,我从来没有真正想过这个问题,我问自己,还有什么所有的数组实现 - 让我检查 的System.Array 使用对象浏览器(因为它是在CLR每个数组的基类),令我惊讶的是,它没有实现 IEnumerable的< T>

The book claims every array implements IEnumerable<T> (obviously, otherwise we couldn't use LINQ to objects on arrays...). When I saw this, I thought to myself that I never really thought about that, and I asked myself what else all arrays implement - so I examined System.Array using the object browser (since it's the base class for every array in the CLR) and, to my surprise, it doesn't implement IEnumerable<T>.

所以我的问题是:在哪里定义的?我的意思是,我怎么能告诉到底是哪接口每个阵列工具?

So my question is: where is the definition? I mean, how can I tell exactly which interfaces every array implements?

推荐答案

从文档(重点煤矿):

[...]数组类实现 System.Collections.Generic.IList&LT; T&GT; 了System.Collections.Generic.ICollection&LT ; T&GT; System.Collections.Generic.IEnumerable&LT; T&GT; 通用接口。 的实现提供给数组在运行时,因此是不可见的文档生成工具。

[...] 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.

编辑:作为JB Evain指出,在他的评论中,只有向量(一维数组)实现了通用接口。至于为什么的多维数组不执行通用接口,我不太清楚,因为它们实施非一般同行(请参见下面的类声明)。

as Jb Evain points out in his comment, only vectors (one-dimensional arrays) implement the generic interfaces. As to why multi-dimensional arrays don't implement the generic interfaces, I'm not quite sure since they do implement the non-generic counterparts (see the class declaration below).

的System.Array 类(即每的数组)还实现了这些非泛型接口:

The System.Array class (i.e. every array) also implements these non-generic interfaces:

public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable