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

C# Temelleri — Switch Case C# Basics — Switch Case

7 dk okuma 7 min read · Emre Ulutabak
1
Switch nedir? What is switch?

Bazen tek bir değere bakıp birçok ihtimalden birini seçmen gerekir. Örneğin haftanın gün numarasına göre gün adını göstermek isteyebilirsin.

Böyle durumlarda switch yapısı kullanılır. Tek bir değeri alır ve uygun eşleşmeye göre ilgili kodu çalıştırır.

Sometimes you need to look at one value and choose one option from many possibilities. For example, you may want to display the day name based on the day number.

That is where switch is used. It takes a single value and runs the matching block of code.

csharp
int gun = 2;

switch (gun)
{
    case 1:
        Console.WriteLine("Pazartesi");
        break;
    case 2:
        Console.WriteLine("Salı");
        break;
}
💡
Switch, tek bir değeri farklı seçeneklerle karşılaştırmak için kullanılır. Switch is used to compare a single value against different options.
2
Neden kullanılır? Why is it used?

Aynı değişken için çok sayıda if / else if yazmak kodu uzatabilir. Özellikle eşitlik bazlı kontrollerde switch daha temiz görünür.

Yani bir değerin hangi seçeneğe denk geldiğini kontrol ediyorsan switch okunabilirliği artırabilir.

Writing many if / else if blocks for the same variable can make the code longer. In equality-based checks, switch often looks cleaner.

So if you are checking which option a single value matches, switch can improve readability.

3
Case mantığı Case logic

Her case, olası bir eşleşmeyi temsil eder. Değer o case ile aynıysa ilgili blok çalışır.

Çoğu klasik kullanımda case sonunda break yazılır. Bu, switch yapısından çıkılmasını sağlar.

Each case represents one possible match. If the value matches that case, the related block runs.

In most classic uses, a break statement is written at the end of the case. This exits the switch structure.

csharp
string mevsim = "Yaz";

switch (mevsim)
{
    case "Kış":
        Console.WriteLine("Mont giy.");
        break;
    case "Yaz":
        Console.WriteLine("Tişört giy.");
        break;
}
💡
Eşleşme bulunduğunda ilgili case çalışır ve break ile çıkılır. When a match is found, the related case runs and exits with break.
4
Default rolü Role of default

Bazen hiçbir case eşleşmez. Böyle durumlar için default bloğu vardır.

Default, 'bunların hiçbiri değilse bunu yap' demektir. Güvenlik ağı gibi düşünebilirsin.

Sometimes none of the cases match. That is what the default block is for.

Default means 'if none of these match, do this'. You can think of it as a safety net.

csharp
int ay = 15;

switch (ay)
{
    case 1:
        Console.WriteLine("Ocak");
        break;
    case 2:
        Console.WriteLine("Şubat");
        break;
    default:
        Console.WriteLine("Geçersiz ay");
        break;
}
💡
Default kullanmak, beklenmeyen durumlarda daha güvenli davranış sağlar. Using default provides safer behavior for unexpected cases.
5
Altın kurallar Golden rules

Switch çok temiz bir yapı sunar ama her yerde kullanılmaz. Doğru yerde kullanıldığında kodu sadeleştirir.

Switch offers a very clean structure, but it is not meant for every situation. When used in the right place, it simplifies the code.

💡
Tek bir değişkenin farklı eşitlik durumlarını kontrol ediyorsan switch mantıklıdır. If you are checking different equality cases of a single variable, switch makes sense.
💡
Koşullar karmaşıksa ve karşılaştırmalar farklı türdeyse if / else daha uygun olabilir. If the conditions are complex and comparisons vary, if / else may be more suitable.
💡
Default bloğunu boş bırakmamaya çalış. Try not to leave the default block empty.
MİNİ QUIZ MINI QUIZ
Aşağıdakilerden hangisi doğrudur? Which of the following is correct?
switch yalnızca döngüler için kullanılır switch is only used for loops
default bloğu hiçbir case eşleşmezse çalışır The default block runs when no case matches
case içinde kod yazılamaz You cannot write code inside a case
switch içinde değer kontrol edilmez No value is checked inside switch