TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
await operator suspends the evaluation of the enclosing asynchronous method or top-level statement until the asynchronous operation represented by its operand completes. When the operation finishes, the await operator returns the result of the operation (if any) and resumes the execution of the context from the suspension point.
Syntax and Context
Theawait operator can be used inside a method, lambda expression, or anonymous function that is explicitly modified by the async keyword. Since C# 9.0, await can also be used directly in top-level statements without any explicit async modifier.
The operator is subject to strict context restrictions. Most notably, await cannot be used inside the body of a lock statement, which results in compiler error CS1996.
Execution Mechanics
When the C# compiler encounters anawait expression, it transforms the enclosing context into a state machine. The mechanical flow is as follows:
- Evaluation: The operator evaluates the operand to obtain an awaitable object.
- Completion Check: It checks if the asynchronous operation has already completed. If true, execution continues synchronously without suspension.
- Suspension: If the operation is pending,
awaitsuspends the execution, captures the current execution context (such as aSynchronizationContextorTaskScheduler), and yields control back to the caller. - Continuation: Once the asynchronous operation completes, the state machine resumes execution on the captured context (unless explicitly bypassed via
ConfigureAwait(false)), and theawaitoperator extracts the result.
The Awaitable Pattern
Theawait operator is not strictly bound to Task or Task<TResult>. It can be applied to any expression that satisfies the awaitable pattern. An object is awaitable if it exposes a GetAwaiter() method—which can be implemented as an instance method or an extension method—that returns an awaiter instance satisfying the following criteria:
- Implements the
System.Runtime.CompilerServices.INotifyCompletionorICriticalNotifyCompletioninterface. - Possesses a boolean
IsCompletedproperty. - Possesses a
GetResult()method.
GetAwaiter() to be an extension method is a fundamental aspect of the pattern, as it enables developers to make types awaitable that they do not own or cannot modify. The following compilable code demonstrates the manual equivalent of the code the compiler generates for an await expression:
Type Unwrapping
The type returned by theawait expression is determined by the return type of the awaiter’s GetResult() method:
- If the operand is
Task<TResult>orValueTask<TResult>, theawaitexpression evaluates toTResult. - If the operand is
TaskorValueTask, theawaitexpression evaluates tovoid.
Exception Handling
When an awaited asynchronous operation faults, the underlying task typically encapsulates the exception within anAggregateException. The await operator intercepts this state, unwraps the AggregateException, and rethrows the first inner exception directly. It utilizes System.Runtime.ExceptionServices.ExceptionDispatchInfo to preserve the original stack trace of the exception from the point of failure to the await expression.
Master C# with Deep Grasping Methodology!Learn More





