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

后端 未结 4 1857
独厮守ぢ
独厮守ぢ 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:19

    In addition to the other answers if you need a little more control over how the enumerator works or if there is a requirement to customize it beyond what the underlying data structure can provide then you can use the yield keyword.

    public class AlbumList : IEnumerable
    {
      public IEnumerator GetEnumerator()
      {
        foreach (Album item in internalStorage)
        {
          // You could use conditional checks or other statements here for a higher
          // degree of control regarding what the enumerator returns.
          yield return item;
        }
      }
    }
    

提交回复
热议问题