C# Yield Return: What is it and how does it work?

Published: 27 April 2021
on channel: Brian Lagunas
56,745
2.7k

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.

GitHub link; https://github.com/brianlagunas/Yield...

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
📖 Blog: http://bit.ly/BrianLagunasBlog


Watch video C# Yield Return: What is it and how does it work? online without registration, duration hours minute second in high quality. This video was added by user Brian Lagunas 27 April 2021, don't forget to share it with your friends and acquaintances, it has been viewed on our site 56,74 once and liked it 2.7 thousand people.