Troubles implementing IEnumerable

后端 未结 7 867
清酒与你
清酒与你 2020-11-29 11:08

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

using System;
using System.Collections.Generic;
using System.Linq;
us         


        
相关标签:
7条回答
  • 2020-11-29 11:47

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

    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];
    }
    
    0 讨论(0)
提交回复
热议问题