C# 数组是否实现IEnumerable<;T>;?

C# 数组是否实现IEnumerable<;T>;?,c#,.net,oop,C#,.net,Oop,我知道基抽象数组类没有将泛型IEnumerable实现为 public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable { ... } 所以当我创建一个派生数组类时,比如int[]I或字符串[]s,它们是否实现了IEnumerable?我怎样才能看到[]?官方word的源代码 数组具有以下属性: 数组可以是一维的、

我知道基抽象数组类没有将泛型IEnumerable实现为

public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
{
 ...
}
所以当我创建一个派生数组类时,比如
int[]I
字符串[]s,它们是否实现了
IEnumerable
?我怎样才能看到
[]

官方word的源代码

数组具有以下属性:

  • 数组可以是一维的、多维的或交错的

  • 创建阵列实例时,将确定维度的数量和每个维度的长度。这些值不能被忽略 在实例的生存期内更改

  • 数值数组元素的默认值设置为零,引用元素设置为null

  • 锯齿数组是数组的数组,因此其元素是引用类型,并初始化为null

  • 数组的索引为零:具有n个元素的数组的索引范围为0到n-1

  • 数组元素可以是任何类型,包括数组类型

  • 数组类型是从抽象基类型数组派生的引用类型。由于此类型实现了
    IEnumerable
    IEnumerable
    , 您可以在C#中的所有数组上使用
    foreach
    迭代。

这里是官方用语

数组具有以下属性:

  • 数组可以是一维的、多维的或交错的

  • 创建阵列实例时,将确定维度的数量和每个维度的长度。这些值不能被忽略 在实例的生存期内更改

  • 数值数组元素的默认值设置为零,引用元素设置为null

  • 锯齿数组是数组的数组,因此其元素是引用类型,并初始化为null

  • 数组的索引为零:具有n个元素的数组的索引范围为0到n-1

  • 数组元素可以是任何类型,包括数组类型

  • 数组类型是从抽象基类型数组派生的引用类型。由于此类型实现了
    IEnumerable
    IEnumerable
    , 您可以在C#中的所有数组上使用
    foreach
    迭代。


数组周围有一个特殊的类型
szarayHelper
-wrapper。以下是源代码:


另外,数组实现了
IList
,它实现了
ICollection
,它实现了
IEnumerable
。因此,数组实现了
IEnumerable

在数组周围有一个特殊的类型
szarayHelper
-wrapper。以下是源代码:


另外,数组实现了
IList
,它实现了
ICollection
,它实现了
IEnumerable
。因此,数组实现了
IEnumerable

,您可以这样简单地检查它:

var type = typeof(int[]); // or any other type
foreach (var @interface in type.GetInterfaces())
    Console.WriteLine(@interface);
结果是:

System.ICloneable
System.Collections.IList
System.Collections.ICollection
System.Collections.IEnumerable
System.Collections.IStructuralComparable
System.Collections.IStructuralEquatable
System.Collections.Generic.IList`1[System.Int32]
System.Collections.Generic.ICollection`1[System.Int32]
System.Collections.Generic.IEnumerable`1[System.Int32]
System.Collections.Generic.IReadOnlyList`1[System.Int32]
System.Collections.Generic.IReadOnlyCollection`1[System.Int32]

您可以这样简单地进行检查:

var type = typeof(int[]); // or any other type
foreach (var @interface in type.GetInterfaces())
    Console.WriteLine(@interface);
结果是:

System.ICloneable
System.Collections.IList
System.Collections.ICollection
System.Collections.IEnumerable
System.Collections.IStructuralComparable
System.Collections.IStructuralEquatable
System.Collections.Generic.IList`1[System.Int32]
System.Collections.Generic.ICollection`1[System.Int32]
System.Collections.Generic.IEnumerable`1[System.Int32]
System.Collections.Generic.IReadOnlyList`1[System.Int32]
System.Collections.Generic.IReadOnlyCollection`1[System.Int32]

也许值得一读。这看起来很像我的答案。太巧了。马特·沃伦确实指出了stackoverflow链接,以便进一步阅读@HansPassant。也许值得一读。这看起来很像我的答案。太巧了。马特·沃伦确实指出了stackoverflow链接以供进一步阅读@HansPassant。你一定会喜欢构造函数
Contract.Assert(错,“嘿!我怎么到这里来的?”)微软的人有幽默感,还有1@TheGeneral我在调查了另一个问题后发现了它:你必须喜欢构造函数
Contract.Assert(false,“嘿!我是怎么来的?”)微软的人有幽默感,还有1@TheGeneral我在研究另一个问题后发现了它:这是对
数组
的一个非常好的总结,但它没有回答“以及如何查看
[]
的源代码?”@一般来说,这就是我们可以在
System.Linq.Enumerable
中使用扩展方法的原因,因为派生的数组类型确实实现了IEnumerable,尽管基数组类没有实现?@Enigmativity,并且在命名中有苍蝇(再次)。从技术上讲,它并没有被编译。这是对
数组
的一个非常好的总结,但它并没有回答“我如何才能看到
[]
的源代码?”@一般来说,这就是我们可以在
System.Linq.Enumerable
中使用扩展方法的原因,因为派生的数组类型确实实现了IEnumerable,尽管基数组类没有实现?@Enigmativity,并且在命名中有苍蝇(再次)。从技术上讲,这并不是真的