Letβs go through async
and await
in C# β the key to asynchronous programming.
π 1. What is Asynchronous Programming?
-
Asynchronous programming allows tasks to run without blocking the main thread.
-
Useful for I/O operations, web requests, file access, or database calls.
-
async
andawait
keywords make it simple and readable compared to callbacks.
π 2. async
Keyword
-
Marks a method as asynchronous
-
Must return either:
-
Task
β for methods that return nothing -
Task<T>
β for methods that return a value -
void
β only for event handlers
-
public async Task DoWorkAsync()
{
await Task.Delay(1000); // Simulate work
Console.WriteLine("Work done!");
}
- Allows awaiting asynchronous operations inside
π 3. await
Keyword
-
Pauses execution of the async method until the awaited task completes
-
Does not block the calling thread
public async Task MainAsync()
{
Console.WriteLine("Start");
await Task.Delay(2000); // Wait 2 seconds asynchronously
Console.WriteLine("End");
}
Output:
Start
...2 seconds...
End
π 4. Example: Async File Reading
using System.IO;
using System.Threading.Tasks;
public async Task ReadFileAsync()
{
using StreamReader reader = new StreamReader("example.txt");
string content = await reader.ReadToEndAsync();
Console.WriteLine(content);
}
-
ReadToEndAsync()
is non-blocking -
Program can do other work while waiting for I/O
π 5. Returning Values
public async Task<int> CalculateSumAsync(int a, int b)
{
await Task.Delay(1000); // Simulate async operation
return a + b;
}
int result = await CalculateSumAsync(5, 10);
Console.WriteLine(result); // 15
- Use
Task<T>
for returning values from async methods
π 6. Async with Parallel Tasks
public async Task FetchDataAsync()
{
Task<string> task1 = Task.Run(() => "Data 1");
Task<string> task2 = Task.Run(() => "Data 2");
string[] results = await Task.WhenAll(task1, task2);
foreach (var r in results) Console.WriteLine(r);
}
Task.WhenAll()
waits for multiple tasks concurrently
π 7. Best Practices
-
Avoid async void except in event handlers
-
Prefer
Task
orTask<T>
return types -
Use
ConfigureAwait(false)
in libraries to avoid context capture -
Handle exceptions with
try-catch
inside async methods
π 8. Summary Table
Keyword | Description |
---|---|
async | Marks a method as asynchronous |
await | Waits for task completion without blocking |
Return Types | Task , Task<T> , void (event handlers only) |
Use Case | I/O, network calls, database operations |
Best Practice | Avoid async void , handle exceptions, return Task |
β Tip:
-
Use async/await to keep your UI responsive and improve throughput in I/O-heavy applications
-
Combine with Task.WhenAll or Task.WhenAny for concurrency