[英]Implementing of IEnumerable<T>
我有一个代码:
public sealed class Sequence : IEnumerable<MyClass>
{
List<MyClass> _elements;
public IEnumerator<MyClass> Getenumerator()
{
foreach (var item in _elements)
{
yield return item;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this._elements.GetEnumerator();
}
}
// ....
Sequence s = new Sequence();
// ...
// filling s with some data
// ...
foreach(MyClass c in s)
{
// some action
}
该代码不想编译。 它没有想要编译。 使用泛型类型'System.Collections.Generic.IEnumerator'需要'1'类型参数
帮我重写MyClass以支持枚举。
我尝试了几种组合,使用谷歌。 无处可用的Enumerable和IEnumerator的例子。
添加行:
using System.Collections;
您想使用System.Collections.IEnumerator
,而不是System.Collections.Generic.IEnumerator<T>
。
听起来你只是缺少一个using
指令,因为IEnumerator
(非泛型的)存在于System.Collections
,而不是System.Collections.Generic
(默认情况下包含在每个源文件的顶部)。
所以只需定义:
using System.Collections;
而且你很高兴去。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.