Home About Lessons Blog On The Mic Contact Projects
Lessons BASIC · C#

C# Basics — Async / Await

10 min read · Emre Ulutabak
1
What is async?

Think of a café. The waiter takes a coffee order, hands it to the machine, and instead of waiting goes to another table. When the coffee is ready, they come back.

This is asynchronous work. While waiting for one task to finish, other work continues. It is essential for keeping a program responsive.

2
Why does it matter?

You are fetching data from a database. This might take 200ms. If you write it synchronously, the program freezes for 200ms — the user can't do anything.

If you write it asynchronously, the program keeps running and the operation completes when the data arrives.

3
Using async / await
csharp
// Senkron — program bekler
public string VeriGetir()
{
    Thread.Sleep(2000); // 2 saniye bekle (program donar)
    return "Veri geldi";
}

// Asenkron — program beklemez
public async Task<string> VeriGetirAsync()
{
    await Task.Delay(2000); // 2 saniye bekle (program çalışmaya devam eder)
    return "Veri geldi";
}

// Kullanım:
public async Task Calistir()
{
    var veri = await VeriGetirAsync();
    Console.WriteLine(veri);
}
💡
async is used when defining a method. await is placed at the point where you actually wait.
4
What is Task?

Task represents the async operation itself. Methods that return a result use Task<T>, methods that don't return anything use Task.

csharp
// Sonuç döndüren async metot
public async Task<int> ToplamHesaplaAsync(int a, int b)
{
    await Task.Delay(100);
    return a + b;
}

// Sonuç döndürmeyen async metot
public async Task KaydetAsync(string veri)
{
    await Task.Delay(100);
    Console.WriteLine($"{veri} kaydedildi.");
}
5
Golden rules
💡
If you write an async method, always use await inside it. Otherwise it runs synchronously.
💡
In ASP.NET Core, controller actions and service methods are written async — you will see this everywhere now.
💡
Don't use .Result or .Wait() — they cause deadlocks. Always use await.
MINI QUIZ
Which is correct about Async/Await?
Async methods always return Task<string>
Using await completely stops the program
With async/await, the program can continue other work while waiting
Using .Result is a safe replacement for await