作为一名新的 .NET 3.5 程序员,我开始学习 LINQ,我发现了一些我以前没有注意到的非常基础的东西:
这本书声称每个数组都实现了IEnumerable<T>
(显然,否则我们不能对数组上的对象使用 LINQ...)。当我看到这个时,我心想我从来没有真正想过这个,我问自己所有数组还实现了什么 - 所以我检查了
System.Array
使用对象浏览器(因为它是 CLR 中每个数组的基类),令我惊讶的是,它没有实现 IEnumerable<T>
.
所以我的问题是:定义在哪里?我的意思是,我如何才能准确判断每个数组实现了哪些接口(interface)?
最佳答案
来自documentation (强调我的):
[...] 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 在他的评论中指出的那样,只有向量(一维数组)实现通用接口(interface)。至于为什么多维数组不实现泛型接口(interface),我不太确定,因为它们确实实现了非泛型对应物(参见下面的类声明)。
System.Array
类(即每个数组)也实现了这些非通用接口(interface):
public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
关于c# - C#中所有的数组都实现了哪些接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4482557/