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”:
Sponsor Me on GitHub:
Get 2 Free Stocks on WeBull (Valued up to $1600 when you deposit $100)
🐦 Twitter: http://bit.ly/BrianLagunasOnTwitter