What if your program could do many things at once without getting stuck waiting?
Why Async and await keywords in C Sharp (C#)? - Purpose & Use Cases
Imagine you are cooking dinner while also trying to answer phone calls and check emails manually, switching tasks one by one without any help.
This manual juggling is slow and tiring. You waste time waiting for the oven or phone call to finish before moving on, and you might forget to check something important.
Using async and await lets your program start a task and then move on to other work without waiting. When the task is done, it comes back to finish it, making everything faster and smoother.
var result = LongRunningOperation(); Console.WriteLine(result);
var result = await LongRunningOperationAsync(); Console.WriteLine(result);
This makes your programs responsive and efficient, handling many things at once without freezing or delays.
Think of a website loading images and data while you scroll smoothly, instead of freezing until everything loads.
Manual waiting blocks progress and wastes time.
Async and await let tasks run in the background.
Programs stay fast and responsive, improving user experience.
Practice
async keyword in C#?Solution
Step 1: Understand the role of
Theasyncasynckeyword marks a method so it can run asynchronously, allowing other work to continue without waiting.Step 2: Differentiate from other options
It does not automatically make code faster or use multiple threads, nor does it pause execution by itself.Final Answer:
To mark a method that can run asynchronously without blocking the caller -> Option BQuick Check:
async marks asynchronous methods = A [OK]
- Thinking async makes code multithreaded automatically
- Confusing async with pausing execution
- Believing async only works with void methods
Solution
Step 1: Recall async method return types
Async methods that return a value must returnTask<T>, whereTis the return type, herestring.Step 2: Check syntax correctness
public async Task<string> GetData() correctly usesasync Task<string>. Other options have wrong order or missingTask.Final Answer:
public async Task<string> GetData() -> Option DQuick Check:
Async methods returning values use Task<T> = A [OK]
- Placing async after return type
- Returning just string instead of Task<string>
- Using async keyword incorrectly in method signature
async Task<int> GetNumberAsync() { await Task.Delay(100); return 5; }
async Task Main() { var result = await GetNumberAsync(); Console.WriteLine(result); }Solution
Step 1: Understand async method behavior
The methodGetNumberAsyncwaits 100ms then returns 5 asynchronously.Step 2: Await the result and print
TheMainmethod awaitsGetNumberAsyncand prints the returned value, which is 5.Final Answer:
5 -> Option AQuick Check:
Await returns method result = 5 [OK]
- Expecting Task object printed instead of value
- Ignoring await causing wrong output
- Thinking async delays block main thread
async Task<int> CalculateAsync() { return 10; }
void Main() { var result = CalculateAsync(); Console.WriteLine(result); }Solution
Step 1: Analyze method call and return type
CalculateAsyncreturns aTask<int>, butMaincalls it withoutawait, soresultis a Task, not int.Step 2: Understand how to get actual value
To get the int value,awaitmust be used, butMainis not async, so this is a problem.Final Answer:
Missing await keyword when calling CalculateAsync -> Option AQuick Check:
Call async method with await = C [OK]
- Calling async method without await
- Expecting async method to return direct value
- Making Main async without proper signature
Solution
Step 1: Understand running tasks concurrently
Starting both tasks without awaiting immediately lets them run in parallel.Step 2: Await both tasks after starting
Awaiting each task after starting both ensures you wait for both to finish and get results efficiently.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.Final Answer:
var task1 = Task1(); var task2 = Task2(); var result1 = await task1; var result2 = await task2; -> Option CQuick Check:
Start tasks first, then await both = B [OK]
- Awaiting tasks one by one causing sequential execution
- Using .Result causing blocking and deadlocks
- Calling tasks multiple times causing duplicate work
Ready to prove it?
Pass the test and this pattern moves unknown
