C#C
C#•4y ago
big OOF

Yield return

I saw this code and got a little confused:
 foreach (var entity in courses)
            yield return EntityToModel.CreateCourseFromEntity(entity);

What is the reasoning behind yield return in this case? Im thinking a normal return would break the foreach and that there are no need for using yield.

When googling i found that the yield can be useful to reduce code(look at attached image for example) but i cant see how it is contributing to that in the case mentined above.

Thanks in advance 🙂
unknown.png
Was this page helpful?