我们的自定义类的数组如何处理与foreach没有实现的IEnumerable? [英] How Array of our custom classes work with foreach without implemented IEnumerable?

查看:150
本文介绍了我们的自定义类的数组如何处理与foreach没有实现的IEnumerable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  MyClass []数组
这个长的标题已经包含了我所有的问题,所以我只是想举个例子。

这个数组如何与Foreach无关地实现IEnumerable接口的方法?

解决方案

数组实现IEnumerable。引用来自文档:


在.NET Framework 2.0版中,
Array类实现了
系统。 Collections.Generic.IList(T),
System.Collections.Generic.ICollection(T),

System.Collections.Generic.IEnumerable(T)
通用接口。
实现在运行时提供给数组
,因此对于文档build
工具不是
。因此,通用的
接口不会出现在Array
类的
声明语法中,并且对于接口成员
没有引用
主题只能通过将数组强制转换为
通用接口类型(显式
接口实现)来访问。当您将
数组转换为这些接口之一时,要注意的关键

,其中add,insert或
成员的元素将元素移除
NotSupportedException 。


This long title already contain all my question so i just want to give example

MyClass[] array

How this array work with Foreach without implement IEnumerable interface's method ?

解决方案

Array implements IEnumerable. Quote from the doc:

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.

这篇关于我们的自定义类的数组如何处理与foreach没有实现的IEnumerable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆