I noticed that List
defines its enumerator as a struct
, while ArrayList
defines its enumerator as a class
. What\'s t
The easiest way to write an enumerator in C# is with the "yield return" pattern. For example.
public IEnumerator<int> 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.
Write it using yield return
.
As to why you might otherwise choose between class
or struct
, if you make it a struct
then it gets boxed as soon as it is returned as an interface, so making it a struct
just causes additional copying to take place. Can't see the point of that!