本文介绍了我如何实现IEnumerator< T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码没有编译,并且抛出以下错误:
$ b

但是,当我删除不明确的方法时,它会一直给出其他错误。



任何人都可以帮忙吗?

  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
{
返回当前;




公共T当前
{
获得
{
尝试
{
T element = vector [actualIndex];
返回元素;
}
catch(IndexOutOfRangeException e)
{
throw new InvalidOperationException(e.Message);
}
}
}
}


解决方案

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


  • 当类实现 IEnumerable< T>

  • 当一个类实现 IEnumerator< T> 时,它会显示我是可以枚举的东西的集合 ,它指出我是一个枚举某物的东西。



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



另外,为了使 foreach 语句工作,关键字中之后的对象必须实现 IEnumerable code>或 IEnumerable< T> 。如果对象只实现 IEnumerator 或 IEnumerator< T> ,那么它将不起作用。



我相信这是您要查找的代码:

  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()
{
返回vector.Take(count).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}


This code is not compiling, and it's throwing the following error:

But when I delete the ambiguous method, it keeps giving other errors.

Can anyone help?

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);
            }
        }
    }
}
解决方案

I think you're misunderstanding IEnumerator<T>. Typically, collections implement IEnumerable<T>, not IEnumerator<T>. You can think of them like this:

  • When a class implements IEnumerable<T>, it is stating "I am a collection of things that can be enumerated."
  • When a class implements IEnumerator<T>, it is stating "I am a thing that enumerates over something."

It is rare (and probably wrong) for a collection to implement IEnumerator<T>. By doing so, you're limiting your collection to a single enumeration. If you try to loop through the collection within a segment of code that's already looping through the collection, or if you try to loop through the collection on multiple threads simultaneously, you won't be able to do it because your collection is itself storing the state of the enumeration operation. Typically, collections (implementing IEnumerable<T>) return a separate object (implementing IEnumerator<T>) and that separate object is responsible for storing the state of the enumeration operation. Therefore, you can have any number of concurrent or nested enumerations because each enumeration operation is represented by a distinct object.

Also, in order for the foreach statement to work, the object after the in keyword, must implement IEnumerable or IEnumerable<T>. It will not work if the object only implements IEnumerator or IEnumerator<T>.

I believe this is the code you're looking for:

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();
    }
}

这篇关于我如何实现IEnumerator&lt; T&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 14:01