What is async and await?
๐ก Concept: async and await
Async and await are keywords in C# that enable writing asynchronous code in a readable and maintainable way.
๐ Quick Intro
They allow methods to run asynchronously without blocking the main thread, improving responsiveness.
๐ง Analogy
Imagine placing an order and continuing other tasks without waiting for the delivery to arrive; async-await lets your program do the same.
๐ง Technical Explanation
- โณ The async modifier marks a method as asynchronous.
- โญ๏ธ The await keyword pauses execution until the awaited Task completes.
- ๐งต Enables non-blocking I/O operations.
- โ๏ธ Works with Task and Task<T> types.
- ๐ Improves scalability and UI responsiveness.
๐ฏ Use Cases
- โ Performing I/O-bound operations asynchronously.
- โ Keeping UI responsive during background tasks.
- โ Implementing scalable web applications.
- โ Avoiding thread blocking in server-side code.
๐ป Code Example
public async Task GetDataAsync() {
await Task.Delay(1000);
return 42;
}
public async Task UseDataAsync() {
int result = await GetDataAsync();
Console.WriteLine($""Result: {result}"");
}

โ Interview Q&A
Q1: What does async keyword do?
A: Marks a method as asynchronous.
Q2: What is the purpose of await?
A: Pauses method execution until Task completes.
Q3: Can async methods return void?
A: Only event handlers.
Q4: Does async create new threads?
A: No, it doesn't create threads but enables asynchronous execution.
Q5: What is Task in async?
A: Represents ongoing work.
Q6: How does async improve UI apps?
A: Prevents blocking the UI thread.
Q7: Can async methods be awaited?
A: Yes, to get result or continue.
Q8: What happens if you forget await?
A: Task runs but exceptions may be unhandled.
Q9: Is async suitable for CPU-bound tasks?
A: Typically no, use parallelism instead.
Q10: What namespace is async part of?
A: System.Threading.Tasks.
๐ MCQs
Q1. What does async keyword do?
- Blocks execution
- Marks method as asynchronous
- Creates thread
- Returns void
Q2. What is the purpose of await?
- Blocks thread
- Pauses until Task completes
- Creates new thread
- Throws error
Q3. Can async methods return void?
- Yes always
- Only event handlers
- No
- Sometimes
Q4. Does async create new threads?
- Yes
- No
- Sometimes
- Always
Q5. What is Task in async?
- Thread
- Process
- Represents ongoing work
- Event
Q6. How does async improve UI apps?
- Slows UI
- Prevents blocking UI thread
- Ignores UI
- Blocks UI
Q7. Can async methods be awaited?
- No
- Yes
- Sometimes
- Never
Q8. What happens if you forget await?
- No effect
- Exceptions unhandled
- Code won't run
- Blocks thread
Q9. Is async suitable for CPU-bound tasks?
- Yes
- No
- Sometimes
- Always
Q10. What namespace is async part of?
- System.IO
- System.Threading.Tasks
- System.Net
- System.Text
๐ก Bonus Insight
Async and await simplify asynchronous programming, making code easier to write, read, and maintain.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!