C# Async Yield Return: IAsyncEnumerable will change your life!!!

preview_player
Показать описание
Have you ever wondered if there was an async yield return in C#? Well, there is an async yield return and in this video, I'll show you how to use the yield return statement asynchronously in C#.

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. The yield return statement will 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.

When working with asynchronous code using async/await, the use of the yield return statement requires just a few small adjustments to your code. Using an async yield return statement requires that the method be asynchronous, making use of async/await.

Usually an async method will return a task. Your first thought when using yield return in your async method may be to have the method return Task of IEnumerable. However, this is not allowed when using yield return, instead your async yield return method must return an IAsyncEnumerable.

You also cannot wait your new async yield return method that returns an IAsynEnumerable. Instead, yo must await the "foreach" statement instead. This is becuase IAsyncEnumerable does not have a GetAwaiter() method. IAsyncEnumerable is an async iterator.

Hopefully this video will help clear up the async yield return usage.

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)

Follow Me:
Рекомендации по теме
Комментарии
Автор

Popped in my feed, didn't search for this video. Congrats, YouTube is recommending your videos for people learning c#.

krzysztofpaszkiewicz
Автор

Oh my goodness thank you so much for answering my question! That's so awesome!

WillEhrendreich
Автор

YOU ARE BOSS... you know that ❤, I have 10 years of experience in .NET Development but this is the first channel I have subscribed which talks about coding :)

swapnilgadge
Автор

Wow, what a great video. You have a gift for explaining things that are not easy at all in a very simple manner. Way to go!

misiekgud
Автор

Hey Brian you are simply great.. Really inspiring for the developers to change the way to write the code efficiently. Thanks.

pratikranchhod
Автор

Very well explained Brian. I like the way how you compare it with your previous video of yield. Keep it up.

abexperience
Автор

You are a star!! Thanx a lot for your tutorials Brian!! Best regards from Greece!!

niangel
Автор

Excellent. Simple and crisp. Showed very well how to use IAsyncEnumerable!

pareshdehadray
Автор

This video was really good; and it made sense. Awesome job Brian.

tcopling
Автор

wow, this is amazing. I expect next video. Many thanks

haule
Автор

Another one over the center field fence! Exactly what I was wondering about.

davidwhite
Автор

Great video. I hope you start making videos again!

whosgotrythm
Автор

Thank you. this was best explained here than anywhere else for my understanding

joeycopperson
Автор

Great explanation from the creator of Prism.. Had been a fan since quite sometime..

Jashobantac
Автор

I use this to stream live FOREX prices from a broker. I'm so glad they put this in there.

dasfahrer
Автор

Great video! I am glad you showed trying to wrap the get lines in a Task<T> since that would have been my first go to attempt.

kickback
Автор

Exactly what I need!! 👌🏼Cristal Clear explanation! 👏🏼👏🏼👏🏼Thanks!

kingbirdfpv
Автор

I love your videos because you get straight to the point

just-jiu
Автор

Great video, didn't know the IAsyncEnumerable 👍

arielunanue
Автор

Thanks a lot for your videos!

1. What to practice in order to be good at understanding complex systems?

2. How to get over the feeling of overwhelming when trying to learn? I always try to rush through a topic knowing that there are other stuff needed to be learnt.

David-qzrd