我该如何实现IEnumerator ?
此代码未编译,并且抛出以下错误:
类型’TestesInterfaces.MyCollection’已包含’Current’的定义
但是当我删除模棱两可的方法时,它会不断给出其他错误。
有人可以帮忙吗?
public class MyCollection : IEnumerator { private T[] vector = new T[1000]; private int actualIndex; public void Add(T elemento) { this.vector[vector.Length] = elemento; } public bool MoveNext() { actualIndex++; return (vector.Length > actualIndex); } public void Reset() { actualIndex = -1; } void IDisposable.Dispose() { } public Object Current { get { return Current; } } public T Current { get { try { T element = vector[actualIndex]; return element; } catch (IndexOutOfRangeException e) { throw new InvalidOperationException(e.Message); } } } }
我认为你误解了IEnumerator
。 通常,集合实现IEnumerable
,而不是IEnumerator
。 你可以这样想:
集合实现IEnumerator
很少(也可能是错误的)。 通过这样做,您将集合限制为单个枚举。 如果您尝试在已经循环遍历集合的代码段中循环遍历集合,或者如果您尝试同时在多个线程上遍历集合,则无法执行此操作,因为您的集合本身正在存储枚举操作的状态。 通常,集合(实现IEnumerable
)返回一个单独的对象(实现IEnumerator
),并且该单独的对象负责存储枚举操作的状态。 因此,您可以拥有任意数量的并发或嵌套枚举,因为每个枚举操作都由不同的对象表示。
另外,为了使foreach
语句起作用, in
关键字之后的对象必须实现IEnumerable
或IEnumerable
。 如果对象仅实现IEnumerator
或IEnumerator
,它将无法工作。
我相信这是您正在寻找的代码:
public class MyCollection : IEnumerable { private T[] vector = new T[1000]; private int count; public void Add(T elemento) { this.vector[count++] = elemento; } public IEnumerator GetEnumerator() { return vector.Take(count).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
您需要定义当前正在实现的接口。
Object IEnumerator.Current { // } public T Current { // }
这样你的类就有2个Current
属性。 但你可以访问它们。
MyCollection col = new MyCollection (); var ienumeratort = col.Current; //Uses IEnumerator var ienumerator = (IEnumerator)col.Current; //uses IEnumerator
我认为从C#2.0开始,你有一个非常简单的方法来实现迭代器,编译器通过创建状态机在场景中做了很多繁重的工作。 值得研究一下。 话虽如此,在这种情况下,您的实现将如下所示:
public class MyCollection { private T[] vector = new T[1000]; private int actualIndex; public void Add(T elemento) { this.vector[vector.Length] = elemento; } public IEnumerable CreateEnumerable() { for (int index = 0; index < vector.Length; index++) { yield return vector[(index + actualIndex)]; } } }
我不确定actualIndex的目的 – 但我希望你能得到这个想法。
在正确初始化MyCollection后,下面的片段看起来有点像消费者的观点:
上述就是C#学习教程:我该如何实现IEnumerator ?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
MyCollection mycoll = new MyCollection (); foreach (var num in mycoll.CreateEnumerable()) { Console.WriteLine(num); }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/961216.html