Ana Sayfa Hakkımda Dersler Blog MikrofondaOn The Mic İletişim Projeler Yakında
DerslerLessons TEMEL · C#

C# Temelleri — Async / Await C# Basics — Async / Await

10 dk okuma 10 min read · Emre Ulutabak
1
Asenkron nedir? What is async?

Bir kafe düşün. Garson kahve siparişini alır, makineye verir ve beklemek yerine diğer masaya gider. Kahve hazır olunca geri döner.

Bu asenkron çalışmadır. Bir işin bitmesini beklerken başka iş yapılır. Programın donmaması, kullanıcıya yanıt vermeye devam etmesi için şarttır.

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
Neden önemli? Why does it matter?

Veritabanından veri çekiyorsun. Bu işlem 200ms sürebilir. Senkron yazarsan program bu 200ms boyunca donar — kullanıcı hiçbir şey yapamaz.

Asenkron yazarsan program çalışmaya devam eder, veri gelince işlem tamamlanır.

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
async / await kullanımı 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 bir metot tanımlarken kullanılır. await ise içinde bekleme yapılan yere konulur. async is used when defining a method. await is placed at the point where you actually wait.
4
Task nedir? What is Task?

Task asenkron bir işlemin kendisini temsil eder. Sonuç döndüren metotlar Task<T>, döndürmeyenler Task kullanır.

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
Altın kurallar Golden rules
💡
async metot yazdıysan içinde mutlaka await kullan. Yoksa senkron gibi çalışır. If you write an async method, always use await inside it. Otherwise it runs synchronously.
💡
ASP.NET Core'da controller action'ları ve servis metotları async yazılır — bunu artık her yerde göreceksin. In ASP.NET Core, controller actions and service methods are written async — you will see this everywhere now.
💡
.Result veya .Wait() kullanma — deadlock yaratır. Her zaman await kullan. Don't use .Result or .Wait() — they cause deadlocks. Always use await.
MİNİ QUIZ MINI QUIZ
Async/Await hakkında hangisi doğrudur? Which is correct about Async/Await?
async metotlar her zaman Task<string> döndürür Async methods always return Task<string>
await kullanmak programı tamamen durdurur Using await completely stops the program
async/await sayesinde program bir işi beklerken başka işlere devam edebilir With async/await, the program can continue other work while waiting
.Result kullanmak await'in yerine geçer ve daha güvenlidir Using .Result is a safe replacement for await