© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
5 replies
big OOF

Yield return

I saw this code and got a little confused:
 foreach (var entity in courses)
            yield return EntityToModel.CreateCourseFromEntity(entity);
 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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

yield return
C#CC# / help
2y ago
✅ Early return with yield
C#CC# / help
11mo ago
yield return in c#
C#CC# / help
2y ago
enumerable.range vs yield return
C#CC# / help
2y ago