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!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: