所有数组在 C# 中都实现了哪些接口? [英] What interfaces do all arrays implement in C#?
问题描述
作为一个新的 .NET 3.5 程序员,我开始学习 LINQ,我发现了一些我以前没有注意到的非常基本的东西:
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
(显然,否则我们无法使用 LINQ 到数组上的对象......).当我看到这个时,我想我从来没有真正想过那个,我问自己所有数组还实现了什么 - 所以我检查了System.Array
使用对象浏览器(因为它是 CLR 中每个数组的基类),令我惊讶的是,它没有实现 IEnumerable
.
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?
推荐答案
来自 文档(强调我的):
[...] Array 类实现了 System.Collections.Generic.IList
、System.Collections.Generic.ICollection
和System.Collections.Generic.IEnumerable
通用接口.实现在运行时提供给数组,因此对文档构建工具不可见.
[...] 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.
正如 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
类(即 every 数组)也实现了这些非通用接口:
The System.Array
class (i.e. every array) also implements these non-generic interfaces:
public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
这篇关于所有数组在 C# 中都实现了哪些接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!