0
0
C Sharp (C#)programming~10 mins

Async and await keywords in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Async and await keywords
Start Main Method
Call Async Method
Async Method Starts
Hit await: Pause Async Method
Return Control to Main
Async Task Completes
Resume Async Method
Async Method Ends
Program Ends
The program starts the main method, calls an async method which pauses at await, returns control to main, then resumes and completes after the awaited task finishes.
Execution Sample
C Sharp (C#)
static async Task Main()
{
    Console.WriteLine("Start");
    await Task.Delay(1000);
    Console.WriteLine("End");
}
This code prints "Start", waits asynchronously for 1 second, then prints "End".
Execution Table
StepActionEvaluationOutput
1Enter Main methodStart MainNo output yet
2Print "Start"Console.WriteLine("Start")Start
3Call Task.Delay(1000) with awaitPause Main, start delay taskNo output
4Return control to caller (if any), Main is pausedAwaiting delayNo output
5Delay task completes after 1 secondTask.Delay finishedNo output
6Resume Main after awaitContinue after awaitNo output
7Print "End"Console.WriteLine("End")End
8Main method endsProgram completesNo output
💡 Main method completes after printing "End" following the awaited delay.
Variable Tracker
VariableStartAfter Step 3After Step 6Final
Main TaskNot startedPaused at awaitResumed after awaitCompleted
Key Moments - 3 Insights
Why does the program print "Start" before waiting?
Because the await pauses the async method only after printing "Start". See execution_table step 2 and 3 where "Start" is printed before the await pauses.
Does the program block and freeze during Task.Delay?
No, the await pauses the async method but returns control to the caller, so the program remains responsive. See execution_table step 4 where control returns while waiting.
When does the code after await run?
It runs only after the awaited task completes. See execution_table step 6 where the method resumes after delay finishes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the program output at step 2?
A"End"
B"Start"
CNo output
DError
💡 Hint
Check the Output column at step 2 in execution_table.
At which step does the async method pause and return control?
AStep 2
BStep 3
CStep 4
DStep 6
💡 Hint
Look for the step where the method is paused and control returns in execution_table.
If Task.Delay was removed, how would the execution_table change?
AThe method would not pause and "End" would print immediately after "Start"
BThe method would pause longer
C"Start" would not print
DThe program would crash
💡 Hint
Consider what happens if await Task.Delay is removed; see variable_tracker for task state.
Concept Snapshot
async and await keywords in C#:
- async marks a method as asynchronous
- await pauses method until awaited Task completes
- Control returns to caller during await
- After await, method resumes
- Enables non-blocking waits
Full Transcript
This example shows how async and await work in C#. The Main method is marked async and calls await Task.Delay(1000). The program prints "Start", then pauses at await without blocking. Control returns to the caller while waiting. After 1 second, the delay completes and the method resumes, printing "End". This allows the program to stay responsive during waits.