C# 实现 IEnumerable<T> 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8760322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:18:27  来源:igfitidea点击:

Troubles implementing IEnumerable<T>

c#

提问by BlackBear

I'm trying to write my own (simple) implementation of List. This is what I did so far:

我正在尝试编写自己的(简单的)List 实现。这是我到目前为止所做的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace provaIEnum
{
    class MyList<T> : IEnumerable<T>
    {
        private T[] _array;
        public int Count { get; private set; }

        public MyList() { /* ... */ }
        public void Add(T element) { /* ... */ }

        // ...

        public IEnumerator<T> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
                yield return _array[i];
        }
    }

I'm getting an error about GetEnumerator though:

我收到了一个关于 GetEnumerator 的错误:

'provaIEnum.Lista' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'provaIEnum.Lista.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'.

'provaIEnum.Lista' 没有实现接口成员 'System.Collections.IEnumerable.GetEnumerator()'。“provaIEnum.Lista.GetEnumerator()”无法实现“System.Collections.IEnumerable.GetEnumerator()”,因为它没有“System.Collections.IEnumerator”的匹配返回类型。

I'm not sure if I understand what VS's trying to tell me and I have no idea how to fix it.

我不确定我是否理解 VS 试图告诉我的内容,我不知道如何解决它。

Thanks for your time

谢谢你的时间

采纳答案by Darin Dimitrov

Since IEnumerable<T>implements IEnumerableyou need to implement this interface as well in your class which has the non-generic version of the GetEnumeratormethod. To avoid conflicts you could implement it explicitly:

由于IEnumerable<T>实现,IEnumerable您还需要在具有GetEnumerator方法的非通用版本的类中实现此接口。为避免冲突,您可以明确实现它:

IEnumerator IEnumerable.GetEnumerator()
{
    // call the generic version of the method
    return this.GetEnumerator();
}

public IEnumerator<T> GetEnumerator()
{
    for (int i = 0; i < Count; i++)
        yield return _array[i];
}

回答by Rich O'Kelly

You'll need to implement the non-generic GetEnumerator method as well:

您还需要实现非通用的 GetEnumerator 方法:

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

Since the IEnumerable<T>interface extends IEnumerable, you have to implement the methods declared on both.

由于IEnumerable<T>接口 extends IEnumerable,您必须实现在两者上声明的方法。

回答by Eric Lippert

Read the error message more carefully; it is telling you exactly what you must do. You did not implement System.Collections.IEnumerable.GetEnumerator.

更仔细地阅读错误信息;它准确地告诉您必须做什么。你没有实施System.Collections.IEnumerable.GetEnumerator

When you implement the genericIEnumerable<T>you have to also implement System.Collections.IEnumerable's GetEnumerator.

当您实现泛型时,IEnumerable<T>您还必须实现System.Collections.IEnumerable的 GetEnumerator。

The standard way to do so is:

这样做的标准方法是:

public IEnumerator<T> GetEnumerator () { whatever }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
    return this.GetEnumerator();
}

回答by Chris Shain

Add the following method:

添加以下方法:

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

回答by Ilya Kogan

There are two overloads of GetEnumerator()that you must implement. One comes from IEnumerable<T>and returns IEnumerator<T>. The other comes from IEnumerable and returns IEnumerator. The override should look like this:

GetEnumerator()您必须实现两个重载。一个来自IEnumerable<T>并返回IEnumerator<T>。另一个来自 IEnumerable 并返回IEnumerator。覆盖应如下所示:

IEnumerator IEnumerable.GetEnumerator()

IEnumerator IEnumerable.GetEnumerator()

This is because IEnumerable<T>implements IEnumerable.

这是因为IEnumerable<T>实现IEnumerable.

回答by Michael Hornfeck

Since your class, MyList<T>inherits IEnumerable<out T>which in turn inherits the non-generic IEnumerable, you have two methods you need to implement:

由于类,MyList<T>继承IEnumerable<out T>这反过来又继承了非一般的IEnumerable,你有两个方法需要实现:

IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

An easy way to do this is when you declare your class:

一个简单的方法是当你声明你的类时:

class MyList<T> : IEnumerable<T>

Right click the IEnumerable<T>text and select Implement Interface > Implement Interface Explictlyfrom the context menu.

右键单击IEnumerable<T>文本并从上下文菜单中选择“实现接口”>“显式实现接口”

回答by RotatingWheel

I got similar problem,though I implemented also IEnumerable and IEnumerable. I was using custom type (Person class) than generic type T. For me the reason was, namespace was not included in the top of file.

我遇到了类似的问题,虽然我也实现了 IEnumerable 和 IEnumerable。我使用的是自定义类型(Person 类)而不是通用类型 T。对我来说,原因是命名空间未包含在文件顶部。

using System.Collections; 

After adding the namespace it was fine for me.

添加命名空间后,对我来说很好。