这段代码没有编译,它抛出了以下错误:
类型“TestesInterfaces.MyCollection”已经包含了“当前”的定义
但是当我删除这个不明确的方法时,它会继续给出其他错误。
有人能帮忙吗?
public class MyCollection<T> : IEnumerator<T>
{
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);
}
}
}
}发布于 2013-08-07 13:25:52
您需要定义当前正在实现的接口。
Object IEnumerator.Current
{
//
}
public T Current
{
//
}这样,类就有2个Current属性。但你可以同时访问它们。
MyCollection<string> col = new MyCollection<string>();
var ienumeratort = col.Current; //Uses IEnumerator<T>
var ienumerator = (IEnumerator)col.Current; //uses IEnumeratorhttps://stackoverflow.com/questions/18104624
复制相似问题