C#: IEnumerable, GetEnumerator, a simple, simple example please!

后端 未结 4 1860
独厮守ぢ
独厮守ぢ 2021-02-05 03:44

Trying to create an uebersimple class that implements get enumerator, but failing madly due to lack of simple / non-functioning examples out there. All I want to do is create a

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-05 04:20

    You can simply return the enumerator returned by List.GetEnumerator:

    public class AlbumList : IEnumerable
    {
        // ...
    
        public IEnumerator GetEnumerator()
        {
            return this.albums.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

提交回复
热议问题