Enumerator Implementation: Use struct or class?

前端 未结 4 841
我在风中等你
我在风中等你 2021-02-19 17:46

I noticed that List defines its enumerator as a struct, while ArrayList defines its enumerator as a class. What\'s t

4条回答
  •  独厮守ぢ
    2021-02-19 18:19

    The easiest way to write an enumerator in C# is with the "yield return" pattern. For example.

    public IEnumerator Example() {
      yield return 1;
      yield return 2;
    }
    

    This pattern will generate all of the enumerator code under the hood. This takes the decision out of your hands.

提交回复
热议问题