c# - 我们的自定义类数组如何在没有实现 IEnumerable 的情况下与 foreach 一起工作?

标签 c# .net foreach ienumerable

这个长标题已经包含了我所有的问题所以我只想举个例子

MyClass[] array

这个数组如何在不实现 IEnumerable 接口(interface)方法的情况下与 Foreach 一起工作?

最佳答案

Array实现 IEnumerable。引用文档:

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). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.

关于c# - 我们的自定义类数组如何在没有实现 IEnumerable 的情况下与 foreach 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3053234/

相关文章:

c# - 在 MVVM 框架中使用按钮动态添加文本框

.net - 使用 xsd.exe 从程序集中的类型生成 XSD,在属性上具有数据注释属性

PHP foreach 循环如果没有结果则跳过

c# - 处理将可变长度结果返回到固定缓冲区的方法的最有效方法是什么?

java - 使用 java 流根据特定检查更新对象

powershell - foreach 语句中的多个类似值

c# - 如何使用 C# 更改 image.source?

单个 MongoDB 实例上的 C# MongoDB 驱动程序事务

c# - 在 C# 中检查用户帐户设置

c# - 从代码隐藏向 JavaScript 函数传递参数