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

  Рет қаралды 53,379

Brian Lagunas

Brian Lagunas

Күн бұрын

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; github.com/brianlagunas/Yield...
Be sure to watch my new Pluralsight course "Introduction to Prism for WPF":
📺 bit.ly/PrismForWpf
Sponsor Me on GitHub:
🙏🏼 bit.ly/SponsorBrianOnGitHub
Get 2 Free Stocks on WeBull (Valued up to $1600 when you deposit $100)
💰 bit.ly/BrianWeBull
Follow Me:
🐦 Twitter: bit.ly/BrianLagunasOnTwitter
📖 Blog: bit.ly/BrianLagunasBlog

Пікірлер: 201
@nickchapsas
@nickchapsas 3 жыл бұрын
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.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Good point. Of course this example comes with the "demo code only" disclaimer 😁
@allanhouston22
@allanhouston22 Жыл бұрын
Unnecessary comment, Nick. The guy is demonstrating yield return, no reason to play a smart ass
@jakebradminster709
@jakebradminster709 Жыл бұрын
@@allanhouston22 It was a fair comment, especially for anyone watching this video that is new to programming concepts.
@allanhouston22
@allanhouston22 Жыл бұрын
@@jakebradminster709 eh, no its not
@duarteribeiro1520
@duarteribeiro1520 11 ай бұрын
​@@allanhouston22 who
@brent8450
@brent8450 Ай бұрын
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.
@shotgunanarms
@shotgunanarms 4 ай бұрын
Great tutorial! I was struggling with wrapping my head around yield return and this cleaned up all my confusion. Keep up the great work!
@BrianLagunas
@BrianLagunas 4 ай бұрын
Thank you so much
@AarPlays
@AarPlays 3 жыл бұрын
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!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Not only that, but you can have code that will run after the yield return statement was hit. It's a really cool feature.
@_nitki_
@_nitki_ 3 жыл бұрын
@@BrianLagunas How does the compiler jump between these functions?
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Well, the compiler technically creates an enumerator
@nothingisreal6345
@nothingisreal6345 Жыл бұрын
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.
@andrewdavid9051
@andrewdavid9051 Жыл бұрын
Great explanation!!! I remember using these in coroutines in game engines, now I understand how they work.
@willac4261
@willac4261 5 ай бұрын
Thank you for this explanation!!
@ahmedhafiz2419
@ahmedhafiz2419 Жыл бұрын
Niiice. Seeing what goes behind the scene is making it easier to understand it
@kasozivincent107
@kasozivincent107 3 жыл бұрын
Thanks Brian. I love the way this video was organized. It made much sense
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching as always my friend
@radeksendecki9922
@radeksendecki9922 2 жыл бұрын
Thank you Brian for this explanation. Short and exactly in point.
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thanks for watching
@RobUttley
@RobUttley 3 жыл бұрын
Great example, nicely explained with the benchmarking too. Thanks!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thank you so much
@tirrellcotton
@tirrellcotton 2 жыл бұрын
Best tutorial I've found that explains yield, thanks!!!
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you so much
@ivannarykin
@ivannarykin 4 ай бұрын
Thank you, Brian. That was really good explanation!
@BrianLagunas
@BrianLagunas 4 ай бұрын
Thank you so much
@maitreyeekar7509
@maitreyeekar7509 2 жыл бұрын
Beautifully explained!! Thanks for this video 🙂
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you so much
@ChrisBoot
@ChrisBoot 3 жыл бұрын
Thank you, another great video. I already use yield but it's always worth learning what you don't know!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thank you for watching and your support.
@Grond73
@Grond73 Жыл бұрын
Great video, excellent, concise explanation. World class!!!
@BrianLagunas
@BrianLagunas Жыл бұрын
Thank you so much
@yuliaazarina6789
@yuliaazarina6789 7 ай бұрын
thank you for this clear explanation!
@BrianLagunas
@BrianLagunas 7 ай бұрын
Thanks for watching
@volimax
@volimax 2 ай бұрын
Very clear explanation, thank you!
@BrianLagunas
@BrianLagunas 2 ай бұрын
Thanks for watching
@Ibrahimnada1995
@Ibrahimnada1995 9 ай бұрын
What a great Video...... Respect
@BrianLagunas
@BrianLagunas 9 ай бұрын
Thank you very much
@teraformerr
@teraformerr 3 күн бұрын
Very very good explanation, thanks a lot
@sergiigolembiovskyi1365
@sergiigolembiovskyi1365 Жыл бұрын
It was a really brief explanation ever to understand so a complicated programming statement. Thank you so much!
@BrianLagunas
@BrianLagunas Жыл бұрын
Thank you for your kind comment
@saifullahkhan3465
@saifullahkhan3465 2 жыл бұрын
Thank you so much Brian 🏅
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thanks for watching
@jkahlon101
@jkahlon101 3 жыл бұрын
This is fantastic, thanks so much Brian :-) I think a crash course on Asynchronous programming by you would be amazing. Is this too broad?
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching. That would be pretty broad, but when I get some time I'll think about what that may look like.
@joakimcarlsson7826
@joakimcarlsson7826 3 жыл бұрын
Thank you so much, really great explanation!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Glad you enjoyed it!
@danielvillalba4457
@danielvillalba4457 2 жыл бұрын
Nice explanation, thanks for sharing
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thanks for watching
@rodeaglerod2715
@rodeaglerod2715 2 жыл бұрын
Thanks so well explained.
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you so much
@davidharrington1133
@davidharrington1133 2 жыл бұрын
Great explanation Brian. Subscribed!
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you very much
@nailalzuhairi2690
@nailalzuhairi2690 2 жыл бұрын
best explanation ever seen on youtube.thanks
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you very much
@ApurvKumar2
@ApurvKumar2 3 жыл бұрын
I knew about yield return earlier but learnt about memory diagnoser and performance comparison... Thanks! 😉
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Great! Thanks for watching
@AmmarTheTrainer
@AmmarTheTrainer 2 жыл бұрын
what an easy explanation... thanks...
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thanks for watching
@mikhail7658
@mikhail7658 2 жыл бұрын
Спасибо за объяснение)
@solvedplus858
@solvedplus858 2 жыл бұрын
Genius as usual many thanks for ur valuable tutorial
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you
@sdasddsasadad2287
@sdasddsasadad2287 3 жыл бұрын
Thank you! it really helped me to get started Csharp!!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
That's great! I'm so glad I could help
@kopilkaiser8991
@kopilkaiser8991 Жыл бұрын
You are a great teacher. Keep it up. Easy, simple, and concise explanation
@BrianLagunas
@BrianLagunas Жыл бұрын
Thank you so much. You are too kind
@eldadmo
@eldadmo 3 жыл бұрын
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
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Great point. I probably should have covered this in my video
@user-uj9mj5qu5f
@user-uj9mj5qu5f 8 ай бұрын
Brian, thanks for your videos! Could you please explain what Covariance and Contravariance in simple?
@Klosra
@Klosra Жыл бұрын
rock solid explanation
@BrianLagunas
@BrianLagunas Жыл бұрын
Thank you very much
@user-wp3zs9tj7r
@user-wp3zs9tj7r 3 жыл бұрын
Thank you, Mister Lagunas!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
You are very welcome
@lennertsoffers3491
@lennertsoffers3491 2 жыл бұрын
Wow I've learned more from this video than from my own teacher about this subject, worthy of a subscribe and a like;)
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you so much
@deckermv7530
@deckermv7530 2 жыл бұрын
Thank you so much for sharing this knowledge!! you are very kind! Have a nice day:)
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you very much
@haule5844
@haule5844 3 жыл бұрын
Thanks, this video very userful
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thank you for watching
@codyeversole8416
@codyeversole8416 3 жыл бұрын
Very helpful. Thank you!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching
@onlyrock1351
@onlyrock1351 2 жыл бұрын
Wow, amazing tutorial.
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you! Cheers!
@scotolivera8207
@scotolivera8207 2 жыл бұрын
wow , very informative thanks
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thanks for watching
@neilvermeulen5283
@neilvermeulen5283 Жыл бұрын
Thanx Brian.
@BrianLagunas
@BrianLagunas Жыл бұрын
Thank you for watching
@erik....
@erik.... 3 жыл бұрын
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.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
That's correct. Thanks for sharing
@WilcoBrouwer
@WilcoBrouwer 2 жыл бұрын
Which course was that if you still remember? I watched one on there on that topic but it wasn't that in-depth. Cheers!
@e-cogs
@e-cogs 2 жыл бұрын
Now I am not afraid of yield :-) Thank you very much.
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thanks for watching. I'm glad I could help
@PaulSebastianM
@PaulSebastianM 2 жыл бұрын
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.
@BrianLagunas
@BrianLagunas 2 жыл бұрын
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.
@rock_0075
@rock_0075 Жыл бұрын
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?
@hussainhussaini2267
@hussainhussaini2267 3 жыл бұрын
So helpful!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
I'm so glad
@ajaysonar662
@ajaysonar662 2 жыл бұрын
Great tutorial, completely understood. Thanks ! (Completed 700 likes)
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you so much!
@MrJPhardy
@MrJPhardy 3 жыл бұрын
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.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Depends on the topic. Usually it takes a day to create the demos, record, edit, and publish a video. Thanks for watching
@henrik3098
@henrik3098 7 ай бұрын
thanks brother from another mother mr
@BrianLagunas
@BrianLagunas 7 ай бұрын
Thank you for watching
@mandivambajavangwe5923
@mandivambajavangwe5923 3 жыл бұрын
Doing the lord‘s work there.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
LOL... that's funny
@marcotaliente4785
@marcotaliente4785 2 ай бұрын
What a great channel 🤩
@BrianLagunas
@BrianLagunas 2 ай бұрын
Thank you!
@BahawalTV
@BahawalTV 3 жыл бұрын
Enumerators and Iterators! very interesting.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching
@krzwyr
@krzwyr 2 жыл бұрын
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.
@fullemptiness
@fullemptiness 3 жыл бұрын
Great video! I'd like a video about benchmarking
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for watching
@adamlasry5225
@adamlasry5225 2 жыл бұрын
Thanks a lot.
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thanks for watching
@arthuran4361
@arthuran4361 3 жыл бұрын
Very Good!!!
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thank you
@shmelly7275
@shmelly7275 Жыл бұрын
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).
@BrianLagunas
@BrianLagunas Жыл бұрын
Wow, thanks an impressive system you’ve built. You should be proud.
@DevDunkStudio
@DevDunkStudio Жыл бұрын
Thanks a lot!
@BrianLagunas
@BrianLagunas Жыл бұрын
Thanks for watching
@josmit1708
@josmit1708 2 ай бұрын
bravo!
@HolyRamanRajya
@HolyRamanRajya Жыл бұрын
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.
@evilroxxx
@evilroxxx Жыл бұрын
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?
@BrianLagunas
@BrianLagunas Жыл бұрын
You return any object when you yield return.
@WillEhrendreich
@WillEhrendreich 3 жыл бұрын
Hey, this was awesome. I was thinking, can you explain how this works with IAsyncEnumerable?
@BrianLagunas
@BrianLagunas 3 жыл бұрын
That is a great suggestion. Since it's new to C# 8, this may be a not-well-known API.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Keep an eye out for tomorrow's video 😁
@ismaelfernandezmendez1664
@ismaelfernandezmendez1664 3 ай бұрын
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.
@fokozuynen2048
@fokozuynen2048 2 жыл бұрын
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.
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Yes 😁
@sumo479
@sumo479 Жыл бұрын
legend
@BrianLagunas
@BrianLagunas Жыл бұрын
Thank you
@tramarthomas6105
@tramarthomas6105 Жыл бұрын
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
@BrianLagunas Жыл бұрын
It’s just a single enumerator that tracks it’s position.
@tramarthomas6105
@tramarthomas6105 Жыл бұрын
@@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
@georget10i
@georget10i 3 жыл бұрын
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?
@BrianLagunas
@BrianLagunas 3 жыл бұрын
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.
@georget10i
@georget10i 3 жыл бұрын
@@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?
@strandloper
@strandloper 3 жыл бұрын
@@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
@BrianLagunas 3 жыл бұрын
@@georget10i Not really. The loop is what is creating each of the 1000 people objects.
@georget10i
@georget10i 3 жыл бұрын
@@BrianLagunas @Steve Crane Ah, OK, got it, gentlemen. Thank you!
@MrAmralaa
@MrAmralaa 3 жыл бұрын
Brain, is there any library for web like prism , I know about mvvm and mvc , what I am asking about is the modularity concept !
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Angular has a concept of modules
@abhichohan8837
@abhichohan8837 2 жыл бұрын
Great
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thank you
@valixt
@valixt Жыл бұрын
Nice explanation. But Visual Studio should have dark color theme.)
@BrianLagunas
@BrianLagunas Жыл бұрын
Heck no. Dark mode is really bad for demos
@nithindominic1807
@nithindominic1807 4 ай бұрын
great!
@BrianLagunas
@BrianLagunas 4 ай бұрын
Thank you
@teratoma.
@teratoma. 2 жыл бұрын
as a java developer i find this mindblowing
@BrianLagunas
@BrianLagunas 2 жыл бұрын
It's been over 15 years since I've done any Java. Is it getting any new language features?
@davidwhite2011
@davidwhite2011 3 жыл бұрын
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?
@strandloper
@strandloper 3 жыл бұрын
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.
@BrianLagunas
@BrianLagunas 3 жыл бұрын
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.
@dasfahrer8187
@dasfahrer8187 Жыл бұрын
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.
@BrianLagunas
@BrianLagunas Жыл бұрын
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
@boukriabdelkhalek6855
@boukriabdelkhalek6855 3 жыл бұрын
Awesome
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thank you
@prabhakaranvelliangiri9655
@prabhakaranvelliangiri9655 3 жыл бұрын
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.
@manudelmarche
@manudelmarche Жыл бұрын
"The Lazy Loop" nice name for a roller coaster (or a restaurant, if you serve calamar rings :-))
@user-nw8oi9vn9y
@user-nw8oi9vn9y 5 ай бұрын
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?
@BrianLagunas
@BrianLagunas 5 ай бұрын
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.
@KunalMukherjee3701
@KunalMukherjee3701 3 жыл бұрын
Next video on Task.continuewith vs await
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Thanks for the suggestion
@tomnolane
@tomnolane 2 жыл бұрын
thanks from Russia
@BrianLagunas
@BrianLagunas 2 жыл бұрын
Thanks for watching
@julianturner6203
@julianturner6203 3 жыл бұрын
Where was the intro? Anyway tech questions are great. Thanks Brian
@BrianLagunas
@BrianLagunas 3 жыл бұрын
I stopped doing the intro. I didn't see any real value in it. Thanks for your continued support
@julianturner6203
@julianturner6203 3 жыл бұрын
@@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.
@adamday5045
@adamday5045 Жыл бұрын
GenXers with the death grip on the goatee
@Brombrom41
@Brombrom41 3 жыл бұрын
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 ?
@BrianLagunas
@BrianLagunas 3 жыл бұрын
That depends on how you write the code.
@yavuzerbas875
@yavuzerbas875 Жыл бұрын
You look like Ismail Baki Tuncer from Turkiye.
@BrianLagunas
@BrianLagunas Жыл бұрын
Sounds like a handsome man 😁
@heuristix77
@heuristix77 4 ай бұрын
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 }
@intertubedude
@intertubedude 3 жыл бұрын
I wonder how many people click on this thinking it has to do with earning interest on crypto 😂
@BrianLagunas
@BrianLagunas 3 жыл бұрын
Hahahahaha!!! 🤣🤣🤣🤣
@sreddyj
@sreddyj 2 жыл бұрын
are you the one who created prism??
@BrianLagunas
@BrianLagunas 2 жыл бұрын
A team created Prism, and I was part of that team. I now own and maintain the project.
@RoyZASTEROiD
@RoyZASTEROiD 2 жыл бұрын
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
@BrianLagunas
@BrianLagunas 2 жыл бұрын
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.
@RoyZASTEROiD
@RoyZASTEROiD 2 жыл бұрын
@@BrianLagunas thanks again for great tutorial
@XZYSquare
@XZYSquare 3 жыл бұрын
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
@BrianLagunas 3 жыл бұрын
I think it's important to understand the problem before talking about how yield return solves it.
@XZYSquare
@XZYSquare 3 жыл бұрын
@@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
@BrianLagunas
@BrianLagunas 3 жыл бұрын
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.
@ZiadTorkey
@ZiadTorkey Жыл бұрын
Very helpful Video
@BrianLagunas
@BrianLagunas Жыл бұрын
Thanks for watching
@mukundmagdum4897
@mukundmagdum4897 11 ай бұрын
thank you ...Great explanation.. i understand concept
@BrianLagunas
@BrianLagunas 10 ай бұрын
Thanks for watching
@shahdel-badrawy
@shahdel-badrawy Жыл бұрын
Great
@BrianLagunas
@BrianLagunas Жыл бұрын
Thanks for watching
Task vs ValueTask: When Should I use ValueTask?
10:43
Brian Lagunas
Рет қаралды 19 М.
C# Async Yield Return: IAsyncEnumerable will change your life!!!
10:13
I MADE A CARDBOARD SWING!#asmr
00:40
HAYATAKU はやたく
Рет қаралды 32 МЛН
Glow Stick Secret (part 2) 😱 #shorts
00:33
Mr DegrEE
Рет қаралды 50 МЛН
c# (Csharp):- What is the use of Yield keyword in c# ?
10:35
.NET Interview Preparation videos
Рет қаралды 272 М.
Unity Coroutines - What? Why? How?
11:03
One Wheel Studio
Рет қаралды 43 М.
The fastest way to cast objects in C# is not so obvious
11:10
Nick Chapsas
Рет қаралды 72 М.
Tame the Power of the Iterator Pattern and yield return in C#
11:36
do you know how "return" works under the hood? (are you SURE?)
5:08
Low Level Learning
Рет қаралды 344 М.
Semaphore Explained C#
12:24
Raw Coding
Рет қаралды 38 М.
IEnumerable 🆚 IEnumerator Interfaces in C#
34:06
tutorialsEU
Рет қаралды 27 М.
Implementing IEnumerable
17:35
Coding Tutorials
Рет қаралды 10 М.
C# Yield - Creating Iterators for beginners
5:52
tutorialsEU - C#
Рет қаралды 7 М.
Introducing GPT-4o
26:13
OpenAI
Рет қаралды 4,1 МЛН
Добавления ключа в домофон ДомРу
0:18
Introducing the all-new iPad Pro | Apple
1:29
Apple
Рет қаралды 37 МЛН
Я Создал Новый Айфон!
0:59
FLV
Рет қаралды 3,5 МЛН