In this video, I explain what the yield return in C# is, how yield return works, and when you would use yield return.

When you use the yield return statement in your code, you indicate that the method, operator, or get accessor in which the yield return keyword appears is an iterator.

You use yield return statement to return one one element in a collection at a time.

The result returned from a yield return iterator method can be consumed by using a foreach statement or LINQ query. Each iteration of the foreach loop calls the iterator method. When a yield return statement is reached in the iterator method, the result is returned and the current location in the iterator method is retained. Upon the execution of the next element in the foreach loop, the iterator method is restarted from the retained location the next time that the iterator method is called.

Essentially, using the yield return statement turns a method into a custom iterator.

Be sure to watch my new Pluralsight course “Introduction to Prism for WPF”:
📺 http://bit.ly/PrismForWpf

Sponsor Me on GitHub:
🙏🏼 http://bit.ly/SponsorBrianOnGitHub

Get 2 Free Stocks on WeBull (Valued up to $1600 when you deposit $100)
💰 http://bit.ly/BrianWeBull

Follow Me:
🐦 Twitter: http://bit.ly/BrianLagunasOnTwitter

Brian Lagunas

View all posts

Add comment

Your email address will not be published. Required fields are marked *

Follow Me

Follow me on Twitter, subscribe to my YouTube channel, and watch me stream live on Twitch.