enumerable.range vs yield return
i can't understand the different bw the iterator that has made by yield return and other iterators(normal iterator, enumerable.range iterator)
5 Replies
Whats the confusion?
you could in theory implement
Enumerable.Range
using yield return as such:
its really not more complicated than thatWhat exactly is the question here? Might it be that you want to understand how a method implemented using
yield return
actually works?first of all they are called enumerators in c#/.net (linq has iterators, buts thats basically an implementation detail for them)
for the difference: for
yield return
stuff the compiler will generate its own type that implements the IEnumerator<T>
and more.
thats quite a bit of code thats actually generated if u look atso when using
Enumerable.Range()
instead, the compiler would not create a new type for it, which is quite beneficial long term because not every library will have its own type to do the same thing