Task vs ValueTask: When Should I use ValueTask?

preview_player
Показать описание
In this video, I address the common what is a Task vs ValueTask question.

When answering the question "When should I use a ValueTask?", you must first understand how a Task works.

A Task serves multiple purposes, but at its core it’s a “promise”. It's an object that represents the eventual completion of some operation. If a method returns a Task, regardless of if the method completes synchronously or asynchronously, the method will always allocate a new Task object in memory. This can become a problem when instances of Task objects are created a lot in methods where high-throughput and performance is a primary concern. The more allocations that are made, the more work the GC (garbage collector) will need to do.

This is where the ValueTask comes in. A ValueTask is a struct that wraps either the result, or the Task returned from an async method. This is extremely helpful in scenarios where the method can return either synchronously or asynchronously. The ValueTask will return just the value in the synchronous code path, or the Task in the asynchronous code path. Returning just the value will result in fewer allocations in memory which will improve not only the memory consumption of the method, but the executing performance as well.

In this video, I demonstrate the difference between a Task vs ValueTask as it relates to behavior. I will also show you the memory allocation and performance differences between a Task vs ValueTask.

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:
Рекомендации по теме
Комментарии
Автор

It is like answering to question What is ValueTask, others return a Task<answer> and only Brian returns a VALUE immediately. Thank you, Brian.

andreikashin
Автор

Best explanation on the internet in this topic

ivandrofly
Автор

Finally someone who knows how to explain clearly. I watched other videos and couldn't comprehend why we need this ValueTask. But thanks to your clear explanation in 10 mins it all makes sense. Thanks a lot.

jacfarjama
Автор

I have spend almost the whole evening to understand how they differ through Microsoft blogpost, and I couldn't figure it out. Thanks for your video, I have completely understood.

mahyarazad
Автор

The best explanation I've seen for valuetask, explained in language I understood. Thank you

ChrisBoot
Автор

The best explanation I've seen for valuetask

amrelsher
Автор

Holy cow!
I was wondering the difference. So I googled it, found an article on MS blog. Did not get it. At some point there was an exemple but I could not compare because, thought there was a chapter on Task before, there was no equivalent of the example. But probably mostly because it downs clarity in too many details I could not comprehend. Maybe because of a lack of background.
But here it is exactly what I need to comprehend. Right to the point as I like, good example. Simple definition.

By 4mn of the video (even before) I got the point of those.

Very much thank you for that.

EminoMeneko
Автор

Awesome explaination. I failed to understand other explainations of this type but yours was clear and easy to understand.

CRBarchager
Автор

You explained the best, out of a few that I have seen on YouTube.

xavhow
Автор

thank you so much this cleared it up for me. Great explanation right to the point very easy to understand. First time I found your channel. It was the video after a Nick chapsas video in the search. Who I also enjoy. For that reason I have subscribed. I’m looking forward to future videos. Thank you Brian.

LoganDunning
Автор

That was a really good explanation, Brian!

eduard.schaefer
Автор

Finally a definitive explanation, thank you very much for your help!

fabrizziocht
Автор

Another brilliant video, hope you will come back to teach us more! Thank you

teraformerr
Автор

This is why I consider u as my guru🙏🙏 explanation is crystal clear 👌

udaysuddhala
Автор

What a teacher! Thank you a lot for your excellent explanation!

ВолодимирДідух-нр
Автор

This was a great explanation! Thank you!

KamelJabber
Автор

Very nice video and great demonstration.

CodingKurzgeschichten
Автор

Great explanation Brian, thank you!
...But wait a second, where is "roll dat intro"?

mr-m
Автор

Thanks for explanation, this is the clearest.

Pocvocem
Автор

Thanks for the great explanation. What happens if we use valuetask when the return type is always a task?

adnanmaqboolkhan
join shbcf.ru