What do async/await keywords really do? #174
-
Hi, The snippet code is from Page 460, Please see the two images. What do async/await keywords really do? ReadAsync signature is:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
ValueTaskThe Like the The async and return valuesWith the sample code, Using the That's why you see a different result type with the I hope this helps. More: Stephen Toub has a great blog post: How Async/Await Really Works in C# |
Beta Was this translation helpful? Give feedback.
ValueTask
The
await
keyword works with every returned awaiter object that implements theGetAwaiter
method. When async/await was introduced, theawait
keyword required aTask
.Like the
Task
class, theValueTask
implements theGetAwaiter
method, and thusawait
can wait onValueTask
as well. The difference is thatTask
is a class and thus an object is created that needs cleanup from the garbage collector, whereasValueTask
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 theValueTask
can reduce the overhead.The
Task
class returns aT…