Bird
Raised Fist0
C Sharp (C#)programming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the async keyword in C#?
easy
A. To make a method run faster by using multiple threads automatically
B. To mark a method that can run asynchronously without blocking the caller
C. To declare a method that returns void only
D. To pause the execution of a method until a task completes

Solution

  1. Step 1: Understand the role of async

    The async keyword marks a method so it can run asynchronously, allowing other work to continue without waiting.
  2. Step 2: Differentiate from other options

    It does not automatically make code faster or use multiple threads, nor does it pause execution by itself.
  3. Final Answer:

    To mark a method that can run asynchronously without blocking the caller -> Option B
  4. Quick Check:

    async marks asynchronous methods = A [OK]
Hint: Async marks methods that run without blocking [OK]
Common Mistakes:
  • Thinking async makes code multithreaded automatically
  • Confusing async with pausing execution
  • Believing async only works with void methods
2. Which of the following is the correct way to declare an asynchronous method that returns a string in C#?
easy
A. public string async GetData()
B. public async string GetData()
C. public Task async<string> GetData()
D. public async Task<string> GetData()

Solution

  1. Step 1: Recall async method return types

    Async methods that return a value must return Task<T>, where T is the return type, here string.
  2. Step 2: Check syntax correctness

    public async Task<string> GetData() correctly uses async Task<string>. Other options have wrong order or missing Task.
  3. Final Answer:

    public async Task<string> GetData() -> Option D
  4. Quick Check:

    Async methods returning values use Task<T> = A [OK]
Hint: Async methods returning values use Task<T> type [OK]
Common Mistakes:
  • Placing async after return type
  • Returning just string instead of Task<string>
  • Using async keyword incorrectly in method signature
3. What will be the output of the following C# code?
async Task<int> GetNumberAsync() { await Task.Delay(100); return 5; }
async Task Main() { var result = await GetNumberAsync(); Console.WriteLine(result); }
medium
A. 5
B. 0
C. Task
D. Compilation error

Solution

  1. Step 1: Understand async method behavior

    The method GetNumberAsync waits 100ms then returns 5 asynchronously.
  2. Step 2: Await the result and print

    The Main method awaits GetNumberAsync and prints the returned value, which is 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Await returns method result = 5 [OK]
Hint: Await unwraps async result to actual value [OK]
Common Mistakes:
  • Expecting Task object printed instead of value
  • Ignoring await causing wrong output
  • Thinking async delays block main thread
4. Identify the error in this code snippet:
async Task<int> CalculateAsync() { return 10; }
void Main() { var result = CalculateAsync(); Console.WriteLine(result); }
medium
A. Missing await keyword when calling CalculateAsync
B. CalculateAsync should return int, not Task<int>
C. Main method must be async to call CalculateAsync
D. CalculateAsync cannot return a value

Solution

  1. Step 1: Analyze method call and return type

    CalculateAsync returns a Task<int>, but Main calls it without await, so result is a Task, not int.
  2. Step 2: Understand how to get actual value

    To get the int value, await must be used, but Main is not async, so this is a problem.
  3. Final Answer:

    Missing await keyword when calling CalculateAsync -> Option A
  4. Quick Check:

    Call async method with await = C [OK]
Hint: Always await async calls to get results [OK]
Common Mistakes:
  • Calling async method without await
  • Expecting async method to return direct value
  • Making Main async without proper signature
5. You want to run two independent async tasks and get their results as soon as both finish. Which code correctly does this in C#?
hard
A. var result1 = await Task1(); var result2 = await Task2();
B. var result1 = Task1().Result; var result2 = Task2().Result;
C. var task1 = Task1(); var task2 = Task2(); var result1 = await task1; var result2 = await task2;
D. await Task.WhenAll(Task1(), Task2()); var result1 = Task1().Result; var result2 = Task2().Result;

Solution

  1. Step 1: Understand running tasks concurrently

    Starting both tasks without awaiting immediately lets them run in parallel.
  2. Step 2: Await both tasks after starting

    Awaiting each task after starting both ensures you wait for both to finish and get results efficiently.
  3. Step 3: Analyze other options

    var result1 = await Task1(); var result2 = await Task2(); awaits sequentially, running tasks one after another. var result1 = Task1().Result; var result2 = Task2().Result; blocks synchronously causing deadlocks. await Task.WhenAll(Task1(), Task2()); var result1 = Task1().Result; var result2 = Task2().Result; awaits but then calls Result again, causing redundant calls.
  4. Final Answer:

    var task1 = Task1(); var task2 = Task2(); var result1 = await task1; var result2 = await task2; -> Option C
  5. Quick Check:

    Start tasks first, then await both = B [OK]
Hint: Start tasks first, then await both to run concurrently [OK]
Common Mistakes:
  • Awaiting tasks one by one causing sequential execution
  • Using .Result causing blocking and deadlocks
  • Calling tasks multiple times causing duplicate work
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard