</>
Skip to content
C# lessons (33/34)

C# — Async

Basic async

public static async Task Main()
{
    string result = await FetchDataAsync();
    Console.WriteLine(result);
}

public static async Task<string> FetchDataAsync()
{
    await Task.Delay(1000);
    return "Data loaded";
}

Multiple tasks

public static async Task Main()
{
    var task1 = FetchAsync("url1");
    var task2 = FetchAsync("url2");

    await Task.WhenAll(task1, task2);

    Console.WriteLine(task1.Result);
    Console.WriteLine(task2.Result);
}

Exception handling

try
{
    var result = await RiskyOperationAsync();
}
catch (HttpRequestException ex)
{
    Console.WriteLine($"HTTP error: {ex.Message}");
}

Mini Practice

Write C# code that:

  1. Creates an async method
  2. Runs multiple tasks concurrently
  3. Handles exceptions in async code
  4. Uses Task.WhenAll

Up Next

In the next lesson, you'll learn about Events — event-driven programming.

Related Topics

Frequently Asked Questions about Async

What is Async in C#?

Async is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Async?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Async.

Why is Async important in C#?

Async is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.