yield return in c#
i can't understand yield return , i wish hear from u what do u say about it.
4 Replies
yield statement - provide the next element in an iterator - C# refe...
Use the yield statement in iterators to provide the next value or signal the end of an iteration
Using yield with a compatible return type converts the method to an iterator returning individual items of a collection. Rather than executing the entire method right away and returning the value as defined by the return type, the actual method returns immediately, generating an object storing the state of the method body that you defined. That body only runs when
MoveNext
is called, executing until the next yield
then pausing, exiting MoveNext
with the yielded item or having MoveNext
return false if `yield break
or the end of the method is reached.does the iterator that yield return made it..is different from other iterators?
In other words if you have an sorted list by a complex index, that is where the reference or native value wont suffice, then you have yield so you can do those mafs
It also helps to hide caching behind it, in like a lazy loader (reader)