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

C# Basics — Switch Case

7 min read · Emre Ulutabak
1
What is switch?

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 is used to compare a single value against different options.
2
Why is it used?

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 logic

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;
}
💡
When a match is found, the related case runs and exits with break.
4
Role of default

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;
}
💡
Using default provides safer behavior for unexpected cases.
5
Golden rules

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

💡
If you are checking different equality cases of a single variable, switch makes sense.
💡
If the conditions are complex and comparisons vary, if / else may be more suitable.
💡
Try not to leave the default block empty.
MINI QUIZ
Which of the following is correct?
switch is only used for loops
The default block runs when no case matches
You cannot write code inside a case
No value is checked inside switch