如何使用generics在C#中实现IEnumerable
我的代码具有一个迭代器内类,该类从Ienumerator接口和向量外部类继承,该类从Ienumerable且可显着的接口继承。
public class Vector<T> : IEnumerable<T> where T : IComparable<T>
我无法正确实现iEnumerator ienumeration.getEnumerator()和ienumerator ienumerable.getEnumerator()方法。这就是我到目前为止的
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
//Iterator iterator = new Iterator(this);
//return iterator;
return new Iterator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
foreach(T item in this)
{
yield return item;
}
}
getEnumerator方法应将迭代器类和向量类连接的 迭代器类具有遵循方法
current()
movenext()
reset() 和迭代器构造函数
私人向量_vector; 公共迭代器(向量矢量) { _vector = vector; }
My code has an iterator inner class which inherits from IEnumerator interface and Vector outer class which inherits from IEnumerable and IComparable interfaces.
public class Vector<T> : IEnumerable<T> where T : IComparable<T>
I am unable to implement IEnumerator IEnumerable.GetEnumerator() and IEnumerator IEnumerable.GetEnumerator() methods properly. This is what I have so far
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
//Iterator iterator = new Iterator(this);
//return iterator;
return new Iterator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
foreach(T item in this)
{
yield return item;
}
}
The getEnumerator method should connect the iterator class and the vector class
The iterator class has follow methods
Current()
MoveNext()
Reset()
and an Iterator constructorprivate Vector _vector;
public Iterator(Vector vector)
{
_vector = vector;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论