Share via

What change does the keyword "await" make?

Shervan360 1,681 Reputation points
2025-03-08T21:56:27.31+00:00

Hello,

Without "await" keyword FileStream.ReadAsync return "Task<int>".

With "await" keyword FileStream.ReadAsync return "int".

Could you please why this happened? What happens behind the scenes?

Screenshot 2025-03-08 224240

Screenshot 2025-03-08 224316

Developer technologies | C#
Developer technologies | C#

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.

0 comments No comments

Answer accepted by question author

Marcin Policht 91,060 Reputation points MVP Volunteer Moderator
2025-03-08T22:17:07.88+00:00

This happens because FileStream.ReadAsync is an asynchronous method that returns a Task<int>, representing a pending operation.

  1. Without await:
    • ReadAsync immediately returns a Task<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(), or await to get the actual integer result when the operation completes.
  2. With await:
    • The await keyword unwraps the Task<int> and retrieves the actual integer value when the asynchronous operation completes.
    • The execution of the calling method is paused at await until the Task<int> completes, and then execution resumes with the result of the operation.

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

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,071 Reputation points
    2025-03-10T16:57:05.1733333+00:00

    the wait keyword is c# syntax sugar. The Task supports registration of a delegate (.ContinueWith()) to be called when completed (or via options when an error).

    the compiler converts code with await to .ContinueWith()

    for example:

    async Task<string> MyMethodAsync()
    {
       string result = await GetValueAsync();
       string s = await dowork(result);
       return s;
    }  
    

    is converted by the C# compiler to:

    Task<string> MyMethodAsync()
    {
       return GetValueAsync().ContinueWith(result => 
       {
          return dowork(result).ContinueWith(s => s);
       });
    }  
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.