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
awaitkeyword works with every returned awaiter object that implements theGetAwaitermethod. When async/await was introduced, theawaitkeyword required aTask.Like the
Taskclass, theValueTaskimplements theGetAwaitermethod, and thusawaitcan wait onValueTaskas well. The difference is thatTaskis a class and thus an object is created that needs cleanup from the garbage collector, whereasValueTaskis 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 theValueTaskcan reduce the overhead.The
Taskclass returns aT…