Тёмный

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

Brian Lagunas
Подписаться 18 тыс.
Просмотров 54 тыс.
50% 1

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

Наука

Опубликовано:

 

27 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 202   
@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 Год назад
​@@allanhouston22 who
@brent8450
@brent8450 2 месяца назад
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.
@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.
@shotgunanarms
@shotgunanarms 5 месяцев назад
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 5 месяцев назад
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
@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
@tirrellcotton
@tirrellcotton 3 года назад
Best tutorial I've found that explains yield, thanks!!!
@BrianLagunas
@BrianLagunas 3 года назад
Thank you so much
@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
@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.
@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
@RobUttley
@RobUttley 3 года назад
Great example, nicely explained with the benchmarking too. Thanks!
@BrianLagunas
@BrianLagunas 3 года назад
Thank you so much
@andrewdavid9051
@andrewdavid9051 Год назад
Great explanation!!! I remember using these in coroutines in game engines, now I understand how they work.
@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
@yuliaazarina6789
@yuliaazarina6789 8 месяцев назад
thank you for this clear explanation!
@BrianLagunas
@BrianLagunas 8 месяцев назад
Thanks for watching
@maitreyeekar7509
@maitreyeekar7509 2 года назад
Beautifully explained!! Thanks for this video 🙂
@BrianLagunas
@BrianLagunas 2 года назад
Thank you so much
@volimax
@volimax 3 месяца назад
Very clear explanation, thank you!
@BrianLagunas
@BrianLagunas 3 месяца назад
Thanks for watching
@Grond73
@Grond73 Год назад
Great video, excellent, concise explanation. World class!!!
@BrianLagunas
@BrianLagunas Год назад
Thank you so much
@ahmedhafiz2419
@ahmedhafiz2419 Год назад
Niiice. Seeing what goes behind the scene is making it easier to understand it
@davidharrington1133
@davidharrington1133 2 года назад
Great explanation Brian. Subscribed!
@BrianLagunas
@BrianLagunas 2 года назад
Thank you very much
@ivannarykin
@ivannarykin 6 месяцев назад
Thank you, Brian. That was really good explanation!
@BrianLagunas
@BrianLagunas 5 месяцев назад
Thank you so 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
@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!
@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.
@nailalzuhairi2690
@nailalzuhairi2690 2 года назад
best explanation ever seen on youtube.thanks
@BrianLagunas
@BrianLagunas 2 года назад
Thank you very much
@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.
@willac4261
@willac4261 6 месяцев назад
Thank you for this explanation!!
@joakimcarlsson7826
@joakimcarlsson7826 3 года назад
Thank you so much, really great explanation!
@BrianLagunas
@BrianLagunas 3 года назад
Glad you enjoyed it!
@e-cogs
@e-cogs 3 года назад
Now I am not afraid of yield :-) Thank you very much.
@BrianLagunas
@BrianLagunas 3 года назад
Thanks for watching. I'm glad I could help
@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
@teraformerr
@teraformerr Месяц назад
Very very good explanation, thanks a lot
@saifullahkhan3465
@saifullahkhan3465 2 года назад
Thank you so much Brian 🏅
@BrianLagunas
@BrianLagunas 2 года назад
Thanks for watching
@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
@user-wp3zs9tj7r
@user-wp3zs9tj7r 3 года назад
Thank you, Mister Lagunas!
@BrianLagunas
@BrianLagunas 3 года назад
You are very welcome
@deckermv7530
@deckermv7530 3 года назад
Thank you so much for sharing this knowledge!! you are very kind! Have a nice day:)
@BrianLagunas
@BrianLagunas 3 года назад
Thank you very much
@danielvillalba4457
@danielvillalba4457 2 года назад
Nice explanation, thanks for sharing
@BrianLagunas
@BrianLagunas 2 года назад
Thanks for watching
@solvedplus858
@solvedplus858 2 года назад
Genius as usual many thanks for ur valuable tutorial
@BrianLagunas
@BrianLagunas 2 года назад
Thank you
@AmmarTheTrainer
@AmmarTheTrainer 2 года назад
what an easy explanation... thanks...
@BrianLagunas
@BrianLagunas 2 года назад
Thanks for watching
@rodeaglerod2715
@rodeaglerod2715 2 года назад
Thanks so well explained.
@BrianLagunas
@BrianLagunas 2 года назад
Thank you so much
@Klosra
@Klosra Год назад
rock solid explanation
@BrianLagunas
@BrianLagunas Год назад
Thank you very much
@Ibrahimnada1995
@Ibrahimnada1995 10 месяцев назад
What a great Video...... Respect
@BrianLagunas
@BrianLagunas 10 месяцев назад
Thank you very much
@ajaysonar662
@ajaysonar662 2 года назад
Great tutorial, completely understood. Thanks ! (Completed 700 likes)
@BrianLagunas
@BrianLagunas 2 года назад
Thank you so much!
@onlyrock1351
@onlyrock1351 2 года назад
Wow, amazing tutorial.
@BrianLagunas
@BrianLagunas 2 года назад
Thank you! Cheers!
@codyeversole8416
@codyeversole8416 3 года назад
Very helpful. Thank you!
@BrianLagunas
@BrianLagunas 3 года назад
Thanks for watching
@mandivambajavangwe5923
@mandivambajavangwe5923 3 года назад
Doing the lord‘s work there.
@BrianLagunas
@BrianLagunas 3 года назад
LOL... that's funny
@henrik3098
@henrik3098 8 месяцев назад
thanks brother from another mother mr
@BrianLagunas
@BrianLagunas 8 месяцев назад
Thank you for watching
@fullemptiness
@fullemptiness 3 года назад
Great video! I'd like a video about benchmarking
@BrianLagunas
@BrianLagunas 3 года назад
Thanks for watching
@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
@neilvermeulen5283
@neilvermeulen5283 Год назад
Thanx Brian.
@BrianLagunas
@BrianLagunas Год назад
Thank you for watching
@hussainhussaini2267
@hussainhussaini2267 3 года назад
So helpful!
@BrianLagunas
@BrianLagunas 3 года назад
I'm so glad
@haule5844
@haule5844 3 года назад
Thanks, this video very userful
@BrianLagunas
@BrianLagunas 3 года назад
Thank you for watching
@mikhail7658
@mikhail7658 2 года назад
Спасибо за объяснение)
@BahawalTV
@BahawalTV 3 года назад
Enumerators and Iterators! very interesting.
@BrianLagunas
@BrianLagunas 3 года назад
Thanks for watching
@scotolivera8207
@scotolivera8207 2 года назад
wow , very informative thanks
@BrianLagunas
@BrianLagunas 2 года назад
Thanks for watching
@marcotaliente4785
@marcotaliente4785 4 месяца назад
What a great channel 🤩
@BrianLagunas
@BrianLagunas 4 месяца назад
Thank you!
@arthuran4361
@arthuran4361 3 года назад
Very Good!!!
@BrianLagunas
@BrianLagunas 3 года назад
Thank you
@DevDunkStudio
@DevDunkStudio Год назад
Thanks a lot!
@BrianLagunas
@BrianLagunas Год назад
Thanks for watching
@user-uj9mj5qu5f
@user-uj9mj5qu5f 9 месяцев назад
Brian, thanks for your videos! Could you please explain what Covariance and Contravariance in simple?
@adamlasry5225
@adamlasry5225 2 года назад
Thanks a lot.
@BrianLagunas
@BrianLagunas 2 года назад
Thanks for watching
@ismaelfernandezmendez1664
@ismaelfernandezmendez1664 4 месяца назад
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.
@manudelmarche
@manudelmarche Год назад
"The Lazy Loop" nice name for a roller coaster (or a restaurant, if you serve calamar rings :-))
@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.
@josmit1708
@josmit1708 3 месяца назад
bravo!
@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?
@valixt
@valixt Год назад
Nice explanation. But Visual Studio should have dark color theme.)
@BrianLagunas
@BrianLagunas Год назад
Heck no. Dark mode is really bad for demos
@sumo479
@sumo479 Год назад
legend
@BrianLagunas
@BrianLagunas Год назад
Thank you
@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.
@nithindominic1807
@nithindominic1807 5 месяцев назад
great!
@BrianLagunas
@BrianLagunas 5 месяцев назад
Thank you
@teratoma.
@teratoma. 3 года назад
as a java developer i find this mindblowing
@BrianLagunas
@BrianLagunas 3 года назад
It's been over 15 years since I've done any Java. Is it getting any new language features?
@abhichohan8837
@abhichohan8837 2 года назад
Great
@BrianLagunas
@BrianLagunas 2 года назад
Thank you
@boukriabdelkhalek6855
@boukriabdelkhalek6855 3 года назад
Awesome
@BrianLagunas
@BrianLagunas 3 года назад
Thank you
@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 😁
@HolyRamanRajya
@HolyRamanRajya 2 года назад
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.
@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
@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.
@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 😁
@adamday5045
@adamday5045 Год назад
GenXers with the death grip on the goatee
@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
@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.
@KunalMukherjee3701
@KunalMukherjee3701 3 года назад
Next video on Task.continuewith vs await
@BrianLagunas
@BrianLagunas 3 года назад
Thanks for the suggestion
@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.
@user-nw8oi9vn9y
@user-nw8oi9vn9y 7 месяцев назад
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 7 месяцев назад
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.
@yavuzerbas875
@yavuzerbas875 Год назад
You look like Ismail Baki Tuncer from Turkiye.
@BrianLagunas
@BrianLagunas Год назад
Sounds like a handsome man 😁
@newbiex11
@newbiex11 23 дня назад
I See states machines where? Everywhere
@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!!! 🤣🤣🤣🤣
@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.
@sreddyj
@sreddyj 3 года назад
are you the one who created prism??
@BrianLagunas
@BrianLagunas 3 года назад
A team created Prism, and I was part of that team. I now own and maintain the project.
@heuristix77
@heuristix77 6 месяцев назад
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 }
@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.
@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
@ZiadTorkey
@ZiadTorkey Год назад
Very helpful Video
@BrianLagunas
@BrianLagunas Год назад
Thanks for watching
@mukundmagdum4897
@mukundmagdum4897 Год назад
thank you ...Great explanation.. i understand concept
@BrianLagunas
@BrianLagunas Год назад
Thanks for watching
@shahdel-badrawy
@shahdel-badrawy Год назад
Great
@BrianLagunas
@BrianLagunas Год назад
Thanks for watching
Далее
Task vs ValueTask: When Should I use ValueTask?
10:43
когда повзрослела // EVA mash
00:40
Просмотров 1,6 млн
What are record types in C# and how they ACTUALLY work
15:36
c# (Csharp):- What is the use of Yield keyword in c# ?
10:35
How IEnumerable can kill your performance in C#
11:02
Просмотров 113 тыс.
Unity Coroutines - What? Why? How?
11:03
Просмотров 44 тыс.
Settling the Biggest Await Async Debate in .NET
14:47
Просмотров 140 тыс.
Machine Code Explained - Computerphile
20:32
Просмотров 109 тыс.
Which do I use, ConfigureAwait True or False?
6:53
Просмотров 35 тыс.
Main filter..
0:15
Просмотров 11 млн