简体   繁体   English

C#IEnumerable实现

[英]C# IEnumerable implementation

I don't think I understand IEnumerable and have done several google search. 我认为我不了解IEnumerable并且已经完成了多个Google搜索。

I'm supposed to make an Container of some objects, where the class Container should handle the updating of the collection the Container represent (add, remove). 我应该为一些对象创建一个Container,其中Container类应该处理Container所代表的集合(添加,删除)的更新。

the collection Container should be and instance of IEnumerable<element> . 集合Container应该是IEnumerable<element>实例。

namespace ....
{
    public class Container : Ienumerable<Elements>
    { 
        List<Elements> _elm = new List<Elements>();
        List<Elements> _add = new List<Elements>();
        List<Elements> _rem = new List<Elements>();

        public voic PendAdd(Element elm)
        {
            _add.Add(elm);
        }

        public voic Pendrem(Element elm)
        {
            _rem.remove(elm);
        }

         ...

        public IEnumerator<Element> GetEnumerator()
        {
            return _elm.GetEnumerator();
        }

    }
}

My problem is that IEnumerable<Elements> is an abstract type and if i make _elm this type i can't handle the state of the Container even if i make it 我的问题是IEnumerable<Elements>是抽象类型,如果我将_elm这种类型,即使我将其设置为Container也无法处理Container的状态

IEnumerable<Element> _elm = new List<Element> ();

VS complains about the error: VS抱怨该错误:

'Container' does not implement interface member 'IEnumerable.GetEnumerator()'. “容器”未实现接口成员“ IEnumerable.GetEnumerator()”。 'Container.GetEnumerator()' cannot implement 'IEnumerable.GetEnumerator()' because it does not have the matching return of 'IEnumerator'. 'Container.GetEnumerator()'无法实现'IEnumerable.GetEnumerator()',因为它没有匹配的'IEnumerator'返回。

I know that there are several answers about IEnumerable but they do not help me. 我知道关于IEnumerable有几个答案,但是它们对我没有帮助。

You are missing the IEnumerable.GetEnumerator() : IEnumerable<T> inherits from IEnumerable . 您缺少IEnumerable.GetEnumerator()IEnumerable<T>IEnumerable继承。 so in your class you need two distinct GetEnumerator() with two distinct return types. 因此在您的类中,您需要两个不同的GetEnumerator()和两个不同的返回类型。

public class Container : IEnumerable<Element>
{
    List<Element> _elm = new List<Element>();
    List<Element> _add = new List<Element>();
    List<Element> _rem = new List<Element>();

    public void PendAdd(Element elm)
    {
        _add.Add(elm);
    }

    public void Pendrem(Element elm)
    {
        _rem.Remove(elm);
    }

    public IEnumerator<Element> GetEnumerator()
    {
        return _elm.GetEnumerator();
    }

    // This is called "explicit interface implementation", see
    // https://msdn.microsoft.com/en-us/library/ms173157.aspx
    IEnumerator IEnumerable.GetEnumerator()
    {
        // will call the "other" GetEnumerator()
        return GetEnumerator();
    }
}

The next time you have to implement an interface, I suggest you right click on the interface in Visual Studio and select the Implement Interface to get the skeleton of all the methods you need. 下次必须实现接口时,建议您在Visual Studio中右键单击该接口,然后选择“ Implement Interface以获取所需的所有方法的框架。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM