C# 如何实现IEnumerator<;T>;?

C# 如何实现IEnumerator<;T>;?,c#,generics,C#,Generics,此代码未编译,并引发以下错误: 类型“TestesInterfaces.MyCollection”已包含“Current”的定义 但是当我删除这个模棱两可的方法时,它会不断地给出其他错误 有人能帮忙吗 public class MyCollection<T> : IEnumerator<T> { private T[] vector = new T[1000]; private int actualIndex; public void Add(T

此代码未编译,并引发以下错误:

类型“TestesInterfaces.MyCollection”已包含“Current”的定义

但是当我删除这个模棱两可的方法时,它会不断地给出其他错误

有人能帮忙吗

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);
            }
        }
    }
}
公共类MyCollection:IEnumerator
{
私有T[]向量=新T[1000];
私人指数;
公共无效添加(T元素)
{
this.vector[vector.Length]=elemento;
}
公共图书馆
{
现实指数++;
返回(vector.Length>actualIndex);
}
公共无效重置()
{
实际指数=-1;
}
void IDisposable.Dispose(){}
公共对象流
{
得到
{
回流;
}
}
公共电流
{
得到
{
尝试
{
T元素=向量[actualIndex];
返回元素;
}
捕获(IndexOutOfRangeException e)
{
抛出新的InvalidOperationException(例如消息);
}
}
}
}

您需要定义当前正在实现的接口

Object IEnumerator.Current
{
    //
}

public T Current
{
    //
}
这样,您的类有2个当前属性。但是你可以同时访问它们

MyCollection<string> col = new MyCollection<string>();
var ienumeratort = col.Current; //Uses IEnumerator<T>
var ienumerator = (IEnumerator)col.Current; //uses IEnumerator
MyCollection col=newmyCollection();
var ienumeratort=列电流//使用IEnumerator
变量ienumerator=(ienumerator)列当前值//使用IEnumerator

我想你误解了
IEnumerator
。通常,集合实现的是
IEnumerable
,而不是
IEnumerator
。你可以这样想:

  • 当一个类实现
    IEnumerable
    时,它表示“我是可以枚举的事物的集合。”
  • 当一个类实现
    IEnumerator
    时,它表示“我是一个枚举某个事物的事物。”
集合实现
IEnumerator
是很少见的(可能是错误的)。通过这样做,您将集合限制为单个枚举。如果尝试在已在集合中循环的代码段中循环集合,或者尝试在多个线程上同时循环集合,则无法执行此操作,因为集合本身正在存储枚举操作的状态。通常,集合(实现
IEnumerable
)返回一个单独的对象(实现
IEnumerator
),该单独的对象负责存储枚举操作的状态。因此,您可以有任意数量的并发或嵌套枚举,因为每个枚举操作都由不同的对象表示

另外,为了使
foreach
语句工作,关键字中
后面的
对象必须实现
IEnumerable
IEnumerable
。如果对象仅实现
IEnumerator
IEnumerator
,则它将不起作用

我相信这就是您正在寻找的代码:

public class MyCollection<T> : IEnumerable<T>
{
    private T[] vector = new T[1000];
    private int count;

    public void Add(T elemento)
    {
        this.vector[count++] = elemento;
    }

    public IEnumerator<T> GetEnumerator()
    {
        return vector.Take(count).GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
公共类MyCollection:IEnumerable
{
私有T[]向量=新T[1000];
私人整数计数;
公共无效添加(T元素)
{
这个.vector[count++]=elemento;
}
公共IEnumerator GetEnumerator()
{
返回vector.Take(count).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}
我认为在C#2.0之后,实现迭代器的方法非常简单,编译器通过创建状态机在幕后完成了大量繁重的工作。值得研究一下。话虽如此,在这种情况下,您的实现将如下所示:

    public class MyCollection<T>
    {
        private T[] vector = new T[1000];
        private int actualIndex;

        public void Add(T elemento)
        {
            this.vector[vector.Length] = elemento;
        }

        public IEnumerable<T> CreateEnumerable()
        {
          for (int index = 0; index < vector.Length; index++)
          {
            yield return vector[(index + actualIndex)];
          }
       }
   }
公共类MyCollection
{
私有T[]向量=新T[1000];
私人指数;
公共无效添加(T元素)
{
this.vector[vector.Length]=elemento;
}
公共IEnumerable CreateEnumerable()
{
for(int index=0;index
虽然我不确定实施指数的目的,但我希望你能理解

在正确初始化MyCollection之后,下面是从消费者角度看的片段:

MyCollection<int> mycoll = new MyCollection<int>();
            foreach (var num in mycoll.CreateEnumerable())
            {
                Console.WriteLine(num);
            }
MyCollection mycoll=new MyCollection();
foreach(mycoll.CreateEnumerable()中的var num)
{
控制台写入线(num);
}

更典型的是将强类型属性公开,并且只对
对象
-类型属性使用显式接口实现。您为什么要实现
IEnumerator
而不是
IEnumerable
?这个实现似乎在问线程安全问题(多个枚举看起来会非常失败)是的,我只是在玩集合来了解内部工作,而不仅仅是如何使用它们:)我不是真的打算使用这个集合我正在创建。。。我这么做只是想了解IEnumerator和IEnumerable的功能。。。无论如何,您的实现抛出“System.Collections.Generic.IEnumerator”需要1个类型参数,您需要使用System.Collections的
使用System.Collections.Generic如果您的代码同时使用了
IEnumerable
IEnumerable
。那是真的,我很抱歉。你关于思考IEnumerator和IEnumerable的例子帮助了我。Thanks@MichaelGunter
这很少见(可能是错误的)对于实现IEnumerator的集合。
这是其中之一吗?@ibubi:No。该示例中的代码显示了我所期望的所有
IEnumerable
/
IEnumerator
实现——分别用于
IEnumerable
IEnumerator
的类。