Great explanation. Here is a tip: Never benchmark code that writes in the console. Console writes are extremely expensive and become the single factor that your code performs poorly by orders of magnitude, which ends up completely masking the true execution time. You can have code that executes in 1 picosecond but a single console write can push that to 1ms completely destroying your results.
for a complete tutorial you should add yield break. Also: you can not yield return from within a try / catch block and if you need to for some reason to return an empty enumeration you can use Enumerable.Empty(); An important point is also to mention that using the same enumeration twice will cause the iteration to be run twice - which can become very expensive. So, if you need to process all members of an enumeration it is usually best to use ToList or ToArray to enforce the full enumeration.
Great video. Another thing that worth mentioning is “yield break” which mean you can iterate on demand but when something happens you can just stop , yield break is simple, when move next is called and yield break called , current value == null
Thanks for this. I just watched a course on Linq on Pluralsight yesterday and for those that don't know this is how some Linq operators work as well. For example if you use Where(x => x.Age==32).Take(5) it will not have to loop the entire collection, just until it finds 5 matches.
Oh interesting! So the compiler actually doesn't fire the method until another method acts upon the object that would contain the return from the method. That's super interesting. Thank you google feed for giving me this video!
So it's basically syntactic sugar for writing a lazy enumerator, in the sense that it lazily evaluates the code needed to push the iterator the to the next position (to actually produce the data on that next pos) and this lazy evaluation causes the code jumps we see while debugging and also permits the enumerator to consume less memory in the enumerator setup phase. But as others more "in tune with the compiler" have pointed out on Twitter, and other places, yield return is more costly in terms of OP speed when you run it all the way, just because of these jumps. With very large collections and big batches of work, the difference can become significant. I think yield return is very rarely used in practice.
Exactly, it is syntactical sugar. I think anytime we talk about a “significant” perf difference, it needs to be measured and compared. Often I see these perf concerns turn out to be nothing of concern.
Hello Brian, How much time do you spend to plan such a video? Your explanations are as always on point! Always a pleasure to watch it, no matter if I know it or not.
I use yield to process thousands of entities / children entities. For example, My scenes update and draw entities on that current scene. Whenever I add an child entity or non-child entities to a scene, they all get stored there. Children never get stored to the parent for performance reasons. An 1024x1024 (tiles are 16x16) map has over 10,000 entities (if any of those entities have children). It saves a ton of performance and my frames are buttery smooth + fps is a constant 60fps (using vsync).
So if you don't have direct access to the database and need to stream over an API, how do you get around the http object accumulating all of the records before returning them as one giant list to the client in .Net 6? I've tried using IEnumerable & IAsyncEnumerable along with await foreach, and it always waits for all records at some point in the pipeline before returning. The database may return results one at a time, and the client may render them one at a time, but a result isn't being rendered in the UI as it's being returned from the database. The http object is feeding them to the client one at a time from it's own collection it accumulated from the database.
Anything over http(s) is a request/response. You can implement paging, or you'll need to use something else like SignalR to push real time updates to the client using long-lived connections
Nice, Thank you, I wonder why the metod GetPeople(1000) doesn't work in the place where it is, but in loop foreach where it doesen' t appear? Maybe compilator has made some optimalisation.
Ngl, I really love Java and I am getting into C#, but this is a game changer. Pretty sure you could do something similar in Java, but probably not with Java's default syntax.
so if i understand is creating a pool of the necessary number requested than a entire inventory for then to be cut, wich improve in speed and computing.
Thanks for the video, quick question if you can answer, can we have normal return and yield return in same method, i mean like having if else statement (in a method) and differnt kinds of reutnrs in them?
Yeah, but if you had to use the list repeatedly, wouldn't it behoove you to create it once and have it handy for multiple uses, even if it did create more elements than necessary in those usages depending on how many times it ends up being needed?
I wouldn’t get hung up on the specific example I used to demo the feature. Focus on the functionality of the feature and use it where it makes sense in your apps.
Amazing explanation, I do have one question though. In the for loop with the yield, does it create an ienumerator for each return, or is it just one big collection thats being added to then returned to the previous method?
@@BrianLagunas so if there's a for loop that should iterate 1 million times, but you only allow it to 1000 times, there's only a collection of 1000 objects that was made? Or are those objects thrown out once you're finished with them
Is there a yield equivalent for EF. Is AsQuerable() as close as we get with out repeated calls to a database and take? Maybe how does this apply to non- in memory stores? Does it already work with linq?
LINQ is basically extension methods on IEnumerable and as a method using yield return returns IEnumerable LINQ does work with it. For example in Brian's sample, if GetPeople took no argument and the loop to 1 million was inside it, then GetPeople().Take(1000) would get the first 1000 people.
Hi Brian thanks for a great explanation. I was wondering if it was possible to utilize the power of yield return when returning an IEnumerable collection encapsulated in a ResponseWrapper object and returned as a IActionResult in the API. You know the response object that has a data collection and pagination and other metadata properties. Is it possible to use this technique or we’re forced to only return IEnumerable type data in which how to return the pagination and other metadata, response headers?
Slight thing on the benchmark part, its a bit misleading to say yield has drastically less time requirements when your case hits the termination logic early. If the termination logic satisfies at the last iteration the time taken would be very similar. Probably applies to memory space too. But yes yield does improve performance a lot on average.
Thank you for the great explanation, Brian! A quick question; you have yield return inside a for loop in "GetPeople" method. If yield return returns on demand, why do we still need that loop? Would a caller not be the one controlling how many objects to return?
The method in my example was responsible for creating the Person objects being returned. Keep in mind that yield return must be used to return an IEnumerable. In my example, the caller was controlling how many objects were returned.
@@BrianLagunas Exactly! So if the caller was controlling (1000). There is no need for the loop inside the GetPeople method to exist because we control how many objects we want from the "outside", rather than passing an argument with how many we want to generate and return in a list, am I understanding it right?
@@georget10i The large loop inside the GetPeople method is simulating a potentially infinite data source. In real use it would be consuming from a stream, file, database, etc. The caller is choosing to consume only the first 1000.
@@BrianLagunas I rolled the intro in my head anyway. The value is to separate the catcher from the content. Maybe you just make a short transition. What so ever, I watch you for the content.
Hey Brain, public void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.Register("UserServices"); containerRegistry.Register("TestService"); } How prism will create instance for 2 classes. Please post an small video explaining.
Hello, first of all big thanks for tutorial, i understand yield is for what but I cant understand why im getting any way 1_000_000 element i have code similar like your. U not showing bottom menus like Locals to understand better about People is have only 1000 element, in my code People showing 1_000_000 element. What is wrong with my code? or which part of this lesson i not understand? namespace CLtest { class Person { public int personID { get; set; } public int personQueue { get; set; } } class MainClass { static void Main() => ProcessPeople(); static void ProcessPeople() { IEnumerable People = GetPeople(1_000_000); foreach(Person p in People) { if (p.personID < 1000) { Console.WriteLine($"ID: {p.personID} | Your queue number: {p.personQueue}"); } else break; } } static IEnumerable GetPeople(int Count) { for(int i = 0; i < Count; i++) { yield return new Person() { personID = i, personQueue = i + 1 }; } } } } screenshot from debugging process of my code in first loop of foreach: i.ibb.co/zV5QBdM/Screenshot-1.png
When you expand the People collection while debugging you are forcing the iteration of all items. If you wait to check the collection until after you finish the loop, the collection count should be correct.
I came here to know what that specific thing in the title says. Im a whole 4 minutes in and and I don't understand what yield return is or what it does. That makes incredibly hard to follow along with what you're talking about
@@BrianLagunas Yeah but i also dont know what problem im looking for without being able to tell apart what may make yield return great for the situation. I need to know what it is and what it does before i will be able to tell what the example has to do with the concept in the first place. Its like taking a trip to a famous landmark without knowing what to look for. Then when we get home you tell us what makes what we just saw great. I could have been looking at something completely different and so i missed the opportunity of understanding what made it great
Unfortunately, my teaching style isn't for everyone. It seems the way I approach concepts doesn't fit the way you like to learn them. Thats okay. I'm sure there are other videos you can find that may be better suited for what you are expecting to see. I hope they can help you understand better than I could. Thanks for your feedback.
This feels tailor made to asynchronous repository methods where you don't want to use an ORM: start your SqlDataReader, while Read yield return new Class() { PropertiesFromCurrentRow }
Great explanation. Here is a tip: Never benchmark code that writes in the console. Console writes are extremely expensive and become the single factor that your code performs poorly by orders of magnitude, which ends up completely masking the true execution time. You can have code that executes in 1 picosecond but a single console write can push that to 1ms completely destroying your results.
Good point. Of course this example comes with the "demo code only" disclaimer 😁
Unnecessary comment, Nick. The guy is demonstrating yield return, no reason to play a smart ass
@@allanhouston22 It was a fair comment, especially for anyone watching this video that is new to programming concepts.
@@jakebradminster709 eh, no its not
@@allanhouston22 who
Great tutorial! I was struggling with wrapping my head around yield return and this cleaned up all my confusion. Keep up the great work!
Thank you so much
for a complete tutorial you should add yield break. Also: you can not yield return from within a try / catch block and if you need to for some reason to return an empty enumeration you can use Enumerable.Empty(); An important point is also to mention that using the same enumeration twice will cause the iteration to be run twice - which can become very expensive. So, if you need to process all members of an enumeration it is usually best to use ToList or ToArray to enforce the full enumeration.
That is incredibly helpful. The idea of using Yield Return when you are unsure how much data you may be going over is especially helpful.
Great video.
Another thing that worth mentioning is “yield break” which mean you can iterate on demand but when something happens you can just stop , yield break is simple, when move next is called and yield break called , current value == null
Great point. I probably should have covered this in my video
Thanks for this. I just watched a course on Linq on Pluralsight yesterday and for those that don't know this is how some Linq operators work as well. For example if you use Where(x => x.Age==32).Take(5) it will not have to loop the entire collection, just until it finds 5 matches.
That's correct. Thanks for sharing
Which course was that if you still remember? I watched one on there on that topic but it wasn't that in-depth. Cheers!
Oh interesting! So the compiler actually doesn't fire the method until another method acts upon the object that would contain the return from the method. That's super interesting. Thank you google feed for giving me this video!
Not only that, but you can have code that will run after the yield return statement was hit. It's a really cool feature.
@@BrianLagunas How does the compiler jump between these functions?
Well, the compiler technically creates an enumerator
You are a great teacher. Keep it up. Easy, simple, and concise explanation
Thank you so much. You are too kind
thank you for this clear explanation!
Thanks for watching
It was a really brief explanation ever to understand so a complicated programming statement. Thank you so much!
Thank you for your kind comment
Thank you Brian for this explanation. Short and exactly in point.
Thanks for watching
Best tutorial I've found that explains yield, thanks!!!
Thank you so much
I knew about yield return earlier but learnt about memory diagnoser and performance comparison... Thanks! 😉
Great! Thanks for watching
Great video, excellent, concise explanation.
World class!!!
Thank you so much
Thanks Brian. I love the way this video was organized. It made much sense
Thanks for watching as always my friend
Thank you, another great video. I already use yield but it's always worth learning what you don't know!
Thank you for watching and your support.
Very clear explanation, thank you!
Thanks for watching
Wow I've learned more from this video than from my own teacher about this subject, worthy of a subscribe and a like;)
Thank you so much
So it's basically syntactic sugar for writing a lazy enumerator, in the sense that it lazily evaluates the code needed to push the iterator the to the next position (to actually produce the data on that next pos) and this lazy evaluation causes the code jumps we see while debugging and also permits the enumerator to consume less memory in the enumerator setup phase. But as others more "in tune with the compiler" have pointed out on Twitter, and other places, yield return is more costly in terms of OP speed when you run it all the way, just because of these jumps. With very large collections and big batches of work, the difference can become significant. I think yield return is very rarely used in practice.
Exactly, it is syntactical sugar. I think anytime we talk about a “significant” perf difference, it needs to be measured and compared. Often I see these perf concerns turn out to be nothing of concern.
Niiice. Seeing what goes behind the scene is making it easier to understand it
Now I am not afraid of yield :-) Thank you very much.
Thanks for watching. I'm glad I could help
This is fantastic, thanks so much Brian :-)
I think a crash course on Asynchronous programming by you would be amazing. Is this too broad?
Thanks for watching. That would be pretty broad, but when I get some time I'll think about what that may look like.
Thank you, Brian. That was really good explanation!
Thank you so much
best explanation ever seen on youtube.thanks
Thank you very much
thank you ...Great explanation.. i understand concept
Thanks for watching
Nice explanation, thanks for sharing
Thanks for watching
Finally, i understand yield return better
Thank you, Mister Lagunas!
You are very welcome
Hello Brian,
How much time do you spend to plan such a video?
Your explanations are as always on point!
Always a pleasure to watch it, no matter if I know it or not.
Depends on the topic. Usually it takes a day to create the demos, record, edit, and publish a video. Thanks for watching
Great explanation!!! I remember using these in coroutines in game engines, now I understand how they work.
Great explanation Brian. Subscribed!
Thank you very much
Great example, nicely explained with the benchmarking too. Thanks!
Thank you so much
what an easy explanation... thanks...
Thanks for watching
Very very good explanation, thanks a lot
Genius as usual
many thanks for ur valuable tutorial
Thank you
Thank you for this explanation!!
Great video!
I'd like a video about benchmarking
Thanks for watching
Thank you! it really helped me to get started Csharp!!
That's great! I'm so glad I could help
Thank you so much Brian 🏅
Thanks for watching
Thanks so well explained.
Thank you so much
I use yield to process thousands of entities / children entities. For example, My scenes update and draw entities on that current scene. Whenever I add an child entity or non-child entities to a scene, they all get stored there. Children never get stored to the parent for performance reasons. An 1024x1024 (tiles are 16x16) map has over 10,000 entities (if any of those entities have children). It saves a ton of performance and my frames are buttery smooth + fps is a constant 60fps (using vsync).
Wow, thanks an impressive system you’ve built. You should be proud.
Brian, thanks for your videos!
Could you please explain what Covariance and Contravariance in simple?
Hey, this was awesome. I was thinking, can you explain how this works with IAsyncEnumerable?
That is a great suggestion. Since it's new to C# 8, this may be a not-well-known API.
Keep an eye out for tomorrow's video 😁
Beautifully explained!! Thanks for this video 🙂
Thank you so much
Thanks, this video very userful
Thank you for watching
Thank you so much for sharing this knowledge!! you are very kind! Have a nice day:)
Thank you very much
So if you don't have direct access to the database and need to stream over an API, how do you get around the http object accumulating all of the records before returning them as one giant list to the client in .Net 6? I've tried using IEnumerable & IAsyncEnumerable along with await foreach, and it always waits for all records at some point in the pipeline before returning. The database may return results one at a time, and the client may render them one at a time, but a result isn't being rendered in the UI as it's being returned from the database. The http object is feeding them to the client one at a time from it's own collection it accumulated from the database.
Anything over http(s) is a request/response. You can implement paging, or you'll need to use something else like SignalR to push real time updates to the client using long-lived connections
What a great Video......
Respect
Thank you very much
as a java developer i find this mindblowing
It's been over 15 years since I've done any Java. Is it getting any new language features?
Nice, Thank you, I wonder why the metod GetPeople(1000) doesn't work in the place where it is, but in loop foreach where it doesen' t appear? Maybe compilator has made some optimalisation.
Thank you so much, really great explanation!
Glad you enjoyed it!
Great tutorial, completely understood. Thanks ! (Completed 700 likes)
Thank you so much!
Ngl, I really love Java and I am getting into C#, but this is a game changer. Pretty sure you could do something similar in Java, but probably not with Java's default syntax.
Enumerators and Iterators! very interesting.
Thanks for watching
wow , very informative thanks
Thanks for watching
rock solid explanation
Thank you very much
Great explanation.
so if i understand is creating a pool of the necessary number requested than a entire inventory for then to be cut, wich improve in speed and computing.
Yes 😁
Thanks for the video, quick question if you can answer, can we have normal return and yield return in same method, i mean like having if else statement (in a method) and differnt kinds of reutnrs in them?
Doing the lord‘s work there.
LOL... that's funny
Yeah, but if you had to use the list repeatedly, wouldn't it behoove you to create it once and have it handy for multiple uses, even if it did create more elements than necessary in those usages depending on how many times it ends up being needed?
I wouldn’t get hung up on the specific example I used to demo the feature. Focus on the functionality of the feature and use it where it makes sense in your apps.
Brain, is there any library for web like prism , I know about mvvm and mvc , what I am asking about is the modularity concept !
Angular has a concept of modules
Wow, amazing tutorial.
Thank you! Cheers!
thanks brother from another mother mr
Thank you for watching
Amazing explanation, I do have one question though. In the for loop with the yield, does it create an ienumerator for each return, or is it just one big collection thats being added to then returned to the previous method?
It’s just a single enumerator that tracks it’s position.
@@BrianLagunas so if there's a for loop that should iterate 1 million times, but you only allow it to 1000 times, there's only a collection of 1000 objects that was made? Or are those objects thrown out once you're finished with them
Is there a yield equivalent for EF. Is AsQuerable() as close as we get with out repeated calls to a database and take? Maybe how does this apply to non- in memory stores? Does it already work with linq?
LINQ is basically extension methods on IEnumerable and as a method using yield return returns IEnumerable LINQ does work with it. For example in Brian's sample, if GetPeople took no argument and the loop to 1 million was inside it, then GetPeople().Take(1000) would get the first 1000 people.
I don't use EF, so I'm not sure about the technical implementation of EF collections. However, @Steve Crane is absolutely correct in his statement.
great demonstration
Hi Brian thanks for a great explanation. I was wondering if it was possible to utilize the power of yield return when returning an IEnumerable collection encapsulated in a ResponseWrapper object and returned as a IActionResult in the API. You know the response object that has a data collection and pagination and other metadata properties. Is it possible to use this technique or we’re forced to only return IEnumerable type data in which how to return the pagination and other metadata, response headers?
You return any object when you yield return.
Thanx Brian.
Thank you for watching
What a great channel 🤩
Thank you!
Nice explanation. But Visual Studio should have dark color theme.)
Heck no. Dark mode is really bad for demos
Slight thing on the benchmark part, its a bit misleading to say yield has drastically less time requirements when your case hits the termination logic early. If the termination logic satisfies at the last iteration the time taken would be very similar. Probably applies to memory space too. But yes yield does improve performance a lot on average.
Thank you for the great explanation, Brian! A quick question; you have yield return inside a for loop in "GetPeople" method. If yield return returns on demand, why do we still need that loop? Would a caller not be the one controlling how many objects to return?
The method in my example was responsible for creating the Person objects being returned. Keep in mind that yield return must be used to return an IEnumerable. In my example, the caller was controlling how many objects were returned.
@@BrianLagunas Exactly! So if the caller was controlling (1000). There is no need for the loop inside the GetPeople method to exist because we control how many objects we want from the "outside", rather than passing an argument with how many we want to generate and return in a list, am I understanding it right?
@@georget10i The large loop inside the GetPeople method is simulating a potentially infinite data source. In real use it would be consuming from a stream, file, database, etc. The caller is choosing to consume only the first 1000.
@@georget10i Not really. The loop is what is creating each of the 1000 people objects.
@@BrianLagunas @Steve Crane Ah, OK, got it, gentlemen. Thank you!
"The Lazy Loop" nice name for a roller coaster (or a restaurant, if you serve calamar rings :-))
Спасибо за объяснение)
Thanks a lot.
Thanks for watching
Where was the intro? Anyway tech questions are great. Thanks Brian
I stopped doing the intro. I didn't see any real value in it. Thanks for your continued support
@@BrianLagunas I rolled the intro in my head anyway. The value is to separate the catcher from the content. Maybe you just make a short transition. What so ever, I watch you for the content.
I See states machines
where?
Everywhere
Thanks a lot!
Thanks for watching
Hey Brain,
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register("UserServices");
containerRegistry.Register("TestService");
}
How prism will create instance for 2 classes. Please post an small video explaining.
Very Good!!!
Thank you
So helpful!
I'm so glad
Next video on Task.continuewith vs await
Thanks for the suggestion
thanks from Russia
Thanks for watching
when using yield return in conjunction with database call to get a bunch of data would it be hitting db every time say a row is returned ?
That depends on how you write the code.
Great
Thank you
Very helpful Video
Thanks for watching
great!
Thank you
are you the one who created prism??
A team created Prism, and I was part of that team. I now own and maintain the project.
I wonder how many people click on this thinking it has to do with earning interest on crypto 😂
Hahahahaha!!! 🤣🤣🤣🤣
GenXers with the death grip on the goatee
Thanks
legend
Thank you
bravo!
Awesome
Thank you
You look like Ismail Baki Tuncer from Turkiye.
Sounds like a handsome man 😁
Hello, first of all big thanks for tutorial, i understand yield is for what but I cant understand why im getting any way 1_000_000 element i have code similar like your. U not showing bottom menus like Locals to understand better about People is have only 1000 element, in my code People showing 1_000_000 element. What is wrong with my code? or which part of this lesson i not understand?
namespace CLtest
{
class Person
{
public int personID { get; set; }
public int personQueue { get; set; }
}
class MainClass
{
static void Main() => ProcessPeople();
static void ProcessPeople()
{
IEnumerable People = GetPeople(1_000_000);
foreach(Person p in People)
{
if (p.personID < 1000) { Console.WriteLine($"ID: {p.personID} | Your queue number: {p.personQueue}"); }
else break;
}
}
static IEnumerable GetPeople(int Count)
{
for(int i = 0; i < Count; i++)
{
yield return new Person() { personID = i, personQueue = i + 1 };
}
}
}
}
screenshot from debugging process of my code in first loop of foreach: i.ibb.co/zV5QBdM/Screenshot-1.png
When you expand the People collection while debugging you are forcing the iteration of all items. If you wait to check the collection until after you finish the loop, the collection count should be correct.
@@BrianLagunas thanks again for great tutorial
I came here to know what that specific thing in the title says. Im a whole 4 minutes in and and I don't understand what yield return is or what it does. That makes incredibly hard to follow along with what you're talking about
I think it's important to understand the problem before talking about how yield return solves it.
@@BrianLagunas Yeah but i also dont know what problem im looking for without being able to tell apart what may make yield return great for the situation. I need to know what it is and what it does before i will be able to tell what the example has to do with the concept in the first place. Its like taking a trip to a famous landmark without knowing what to look for. Then when we get home you tell us what makes what we just saw great. I could have been looking at something completely different and so i missed the opportunity of understanding what made it great
Unfortunately, my teaching style isn't for everyone. It seems the way I approach concepts doesn't fit the way you like to learn them. Thats okay. I'm sure there are other videos you can find that may be better suited for what you are expecting to see. I hope they can help you understand better than I could. Thanks for your feedback.
This feels tailor made to asynchronous repository methods where you don't want to use an ORM: start your SqlDataReader, while Read yield return new Class() { PropertiesFromCurrentRow }
Very helpful. Thank you!
Thanks for watching