Async and Await in C# — Interview Guide
async and await are used in C# to write asynchronous, non-blocking code in a simple and readable way. They are heavily used in .NET applications like Web APIs, UI apps, and file/network operations.
1. Why Async/Await is Needed (Interview Answer)
Normally, when your code calls a slow operation (API call, DB query, file read):
-
Synchronous code → blocks the thread (bad for performance)
-
Asynchronous code → frees the thread while waiting (better performance)
Example (Synchronous — Bad)
public string GetData()
{
Thread.Sleep(5000); // Simulate long operation
return "Data received";
}
Problem:
-
Thread blocked for 5 seconds
-
Bad for UI apps and Web APIs
2. Async/Await Basic Example
Async Version
public async Task<string> GetDataAsync()
{
await Task.Delay(5000);
return "Data received";
}
Calling Method
public async Task CallMethod()
{
string data = await GetDataAsync();
Console.WriteLine(data);
}
What Happens Internally
-
GetDataAsync()starts -
await Task.Delay(5000)pauses method -
Thread is freed
-
After 5 seconds → method resumes
-
Result returned
3. Real-World Example (Web API)
This is a very common interview example.
public async Task<IActionResult> GetUsers()
{
var users = await _dbContext.Users.ToListAsync();
return Ok(users);
}
Why async?
-
Database call takes time
-
Thread shouldn't wait
-
Better scalability
4. Return Types in Async Methods (Important Interview Question)
| Return Type | When to Use |
|---|---|
Task |
No return value |
Task<T> |
Return value |
void |
Only for event handlers (avoid otherwise) |
Examples
Task
public async Task DoWorkAsync()
{
await Task.Delay(1000);
}
Task
public async Task<int> GetNumberAsync()
{
await Task.Delay(1000);
return 10;
}
async void (Avoid)
public async void Button_Click()
{
await Task.Delay(1000);
}
5. async vs await (Interview Question)
| Keyword | Purpose |
|---|---|
| async | Marks method as asynchronous |
| await | Waits for async operation without blocking |
Example:
public async Task Method()
{
await Task.Delay(1000);
}
6. Without async/await (Old Way)
Before async/await:
public Task<string> GetData()
{
return Task.Run(() =>
{
Thread.Sleep(5000);
return "Data";
});
}
Hard to read and maintain.
7. Multiple Async Calls (Important Interview Scenario)
Sequential Execution (Slow)
var data1 = await GetData1();
var data2 = await GetData2();
Parallel Execution (Fast)
var task1 = GetData1();
var task2 = GetData2();
await Task.WhenAll(task1, task2);
Interview Tip:
Use Task.WhenAll() for better performance.
8. Common Interview Questions
Q1: Does async create new thread?
Answer:
No.
Async does not create new thread. It uses existing thread efficiently.
Q2: Can we use await without async?
No.
Wrong:
public Task Method()
{
await Task.Delay(1000); // Error
}
Correct:
public async Task Method()
{
await Task.Delay(1000);
}
Q3: What happens if we don't use await?
GetDataAsync(); // Fire and forget
Problem:
-
Exceptions may be lost
-
Hard to debug
9. Exception Handling in Async
try
{
await GetDataAsync();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Works same as synchronous code.
10. Real Interview Example
public async Task<string> DownloadDataAsync()
{
using(var client = new HttpClient())
{
var result = await client.GetStringAsync("https://example.com");
return result;
}
}
11. Best Practices (Interview Gold Points)
✔ Use async Task instead of async void
✔ Use await always unless necessary
✔ Use Task.WhenAll() for parallel calls
✔ Avoid .Result and .Wait() (deadlocks)
Bad:
var data = GetDataAsync().Result;
12. Async Await Flow (Simple Understanding)
Start method
↓
Hit await
↓
Method paused
↓
Thread released
↓
Task completes
↓
Method resumes
One-Line Interview Answer
"Async and await in C# are used to write non-blocking asynchronous code, improving performance and responsiveness by freeing the thread while waiting for long-running operations."
