An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
This happens because FileStream.ReadAsync is an asynchronous method that returns a Task<int>, representing a pending operation.
- Without
await:-
ReadAsyncimmediately returns aTask<int>, which acts as a promise for the future completion of the read operation. - The calling code receives this
Task<int>and can later use.Result,.Wait(), orawaitto get the actual integer result when the operation completes.
-
- With
await:- The
awaitkeyword unwraps theTask<int>and retrieves the actual integer value when the asynchronous operation completes. - The execution of the calling method is paused at
awaituntil theTask<int>completes, and then execution resumes with the result of the operation.
- The
Effectively, await does not make the operation synchronous. Instead, it allows the current method to return control to the caller while waiting for the asynchronous operation to finish. The compiler transforms an async method into a state machine, handling continuations when the awaited Task<int> completes.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin