gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:55:35 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com