简体   繁体   English

有关IEnumerable.GetEnumerator()的错误消息

[英]Error message regarding IEnumerable.GetEnumerator()

I get this error message and I can't figure out why! 我收到此错误消息,我无法弄清楚原因!

Error   1   'Exo5Chap12.ShortCollection<T>' does not implement interface member 
'System.Collections.IEnumerable.GetEnumerator()'. 
'Exo5Chap12.ShortCollection<T>.GetEnumerator()' cannot implement 
'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching 
return type of 'System.Collections.IEnumerator'.    
E:\MyFolders\Dev\c#\Chapter12\Exo5Chap12\Exo5Chap12\exo5.cs 9   18  Exo5Chap12

Here is the code with an implementation of GetEnumerator(). 以下是具有GetEnumerator()实现的代码。 What is wrong? 怎么了?

 public class ShortCollection<T> : IList<T>
{
    protected Collection<T> innerCollection;
    protected int maxSize = 10;
    public IEnumerator<T> GetEnumerator()
    {
        return (innerCollection as IEnumerator<T>).GetEnumerator();
    }
}

As others have said, you need to implement IEnumerable as well as IEnumerable<T> . 正如其他人所说,你需要实现IEnumerable以及IEnumerable<T> However, since IEnumberable<T> itself implemets IEnumerable this is trivial, just call your generic GetEnumerator() : 但是,由于IEnumberable<T>本身实现了IEnumerable这是微不足道的,只需调用你的通用GetEnumerator()

public class ShortCollection<T> : IList<T>
{
    protected Collection<T> innerCollection;
    protected int maxSize = 10;
    public IEnumerator<T> GetEnumerator()
    {
        return innerCollection.GetEnumerator();
    }

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

I'm assuming that you have methods for actually adding and removing from the innerCollection and just left them out for brevity since they didn't relate to the question at hand. 我假设您有实际添加和删除innerCollection方法,为了简洁而将它们排除在外,因为它们与手头的问题无关。

IEnumerable and IEnumerable<T> are different interfaces and your code has to implement both. IEnumerableIEnumerable<T>是不同的接口,你的代码必须同时实现。 Your code only implements the GetEnumerator() of IEnumerable<T> , but not GetEnumerator() of IEnumerable . 您的代码仅实现IEnumerable<T> GetEnumerator(),但不实现IEnumerable GetEnumerator()。 You should consider installing ReSharper which makes it easy to fix errors like this one. 你应该考虑安装ReSharper,它可以很容易地修复像这样的错误。

You need to implement a few more things: 你需要实现更多的东西:

public class ShortCollection<T> : IList<T>
{
    protected Collection<T> innerCollection;
    protected int maxSize = 10;

    #region IList<T> Members

    public int IndexOf(T item)
    {
        return innerCollection.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        innerCollection.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        innerCollection.RemoveAt(index);
    }

    public T this[int index]
    {
        get
        {
            return innerCollection[index];
        }
        set
        {
            innerCollection[index] = value;
        }
    }

    #endregion

    #region ICollection<T> Members

    public void Add(T item)
    {
        innerCollection.Add(item);
    }

    public void Clear()
    {
        innerCollection.Clear();
    }

    public bool Contains(T item)
    {
        return innerCollection.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        innerCollection.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return innerCollection.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(T item)
    {
        return innerCollection.Remove(item);
    }

    #endregion

    #region IEnumerable<T> Members

    public IEnumerator<T> GetEnumerator()
    {
        return innerCollection.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return innerCollection.GetEnumerator();
    }

    #endregion
}

This code will compile... :) 这段代码会编译...... :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在包装字典类中实现IEnumerable.GetEnumerator() - Implementing IEnumerable.GetEnumerator() in a wrapper dictionary class 实现IEnumerable <T> 和IEnumerable.GetEnumerator()无法公开,为什么? - implementing IEnumerable<T> and IEnumerable.GetEnumerator() can not be public, why? 如果 GetEnumerator() 的返回类型不同,IEnumerable.GetEnumerator() 的首选实现如何返回 GetEnumerator()? - How can IEnumerable.GetEnumerator()'s go-to implemetation is to return GetEnumerator(), if GetEnumerator() is of diffrent return type? C#:你如何测试IEnumerable.GetEnumerator()方法? - C#: How do you test the IEnumerable.GetEnumerator() method? 何时调用IEnumerable.GetEnumerator而不是IEnumerable <T> .GetEnumerator? - When does IEnumerable.GetEnumerator get called instead of IEnumerable<T>.GetEnumerator? 动态生成的类,实现IEnumerator <T> GetEnumerator()和IEnumerator IEnumerable.GetEnumerator() - Dynamically generated class that implements IEnumerator<T> GetEnumerator() and IEnumerator IEnumerable.GetEnumerator() 实现 IEnumerable,但避免 &#39;MyClass&#39; 没有实现接口成员 &#39;IEnumerable.GetEnumerator()&#39; - Implement IEnumerable, but avoid 'MyClass' does not implement interface member 'IEnumerable.GetEnumerator()' IEnumerable的 <T> GetEnumerator()执行 - IEnumerable<T> GetEnumerator() exexcution 为什么要列出<t>声明 GetEnumerator() 和 IEnumerable<t> .GetEnumerator()?</t></t> - Why does List<T> declare GetEnumerator() and IEnumerable<T>.GetEnumerator()? IEnumerable - 从局部变量使用GetEnumerator实现 - IEnumerable - implementing using GetEnumerator from local variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM