Skip to content

What do async/await keywords really do? #174

Answered by christiannagel
ShervanN asked this question in Q&A
Discussion options

You must be logged in to vote

ValueTask

The await keyword works with every returned awaiter object that implements the GetAwaiter method. When async/await was introduced, the await keyword required a Task.

Like the Task class, the ValueTask implements the GetAwaiter method, and thus await can wait on ValueTask as well. The difference is that Task is a class and thus an object is created that needs cleanup from the garbage collector, whereas ValueTask is a struct. Compared to an asynchronous invocation, an object is a small overhead. However, if you get a lengthy list, e.g. with asynchronous streaming, not every returned item is really asynchronous, thus the ValueTask can reduce the overhead.

The Task class returns a T…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ShervanN
Comment options

Answer selected by ShervanN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants