3 min read

Switches

The Crash Course

Introduction

While if statements are the main decision making tool, there’s a second tool called a switch that can be pretty useful as well.

A switch is useful in situations where you have a long chain of if/else statements back-to-back.

Consider this if statement, which is something of a continuation from the previous tutorial:

if (stars == 0)
    Console.WriteLine("You got 0 stars. Try again.");
else if (stars == 1)
    Console.WriteLine("You got 1 star. Nice!");
else if (stars == 2)
    Console.WriteLine("You got 2 stars. Fantastic!");
else
    Console.WriteLine("You got 3 stars. Perfect!");

In situations like this, where things fit into one of several buckets based on some simple conditions, you can use a switch instead.

Switch Statements

Switches come in two flavors, a switch statement and a switch expression .

The switch statement equivalent of the above if/else code looks like this:

switch (stars)
{
    case 0:
        Console.WriteLine("You got 0 stars. Try again.");
        break;
    case 1:
        Console.WriteLine("You got 1 star. Nice!");
        break;
    case 2:
        Console.WriteLine("YOu got 2 stars. Fantastic!");
        break;
    default:
        Console.WriteLine("You got 3 stars. Perfect!");
        break;
}

The whole switch statement begins with the switch keyword. Then, in a set of parentheses, you place the value you want to switch on.

Each possible pathway begins with a case statement, which includes defining the value to check it for.

Inside of the body of each arm is the set of statements you wish to run if the data lands in the given category.

Each arm ends with a break; statement, which signifies that the end of the code for that particular case is over.

It is usually a good idea to include a default arm. This is like an else, and is the one that runs if none of the others are a match.

If multiple arms would run the exact same code, you can combine them:

switch (stars)
{
    case 0:
        Console.WriteLine("That wasn't enough to pass.");
        break;
    case 1:
    case 2:
    case 3:
        Console.WriteLine("That's good enough to pass!");
        break;
    default:
        Console.WriteLine("Something has gone horribly wrong.");
        break;
}

Switch Expressions

If your entire switch amounts to picking a value based on some other values–something that can be determined by an expression instead of a full statement–then you could use a switch expression instead.

Let’s rearrange the switch statement from a moment ago and use a switch expression instead:

string output;

output = stars switch
{
    0 => "You got 0 stars. Try again.",
    1 => "You got 1 star. Nice!",
    2 => "You got two stars. Fantastic!",
    _ => "You got three stars. Perfect!"
};

Console.WriteLine(output);

Switch expressions have a lot of similarities to switch statements, but tend to remove a lot of extra overhead. There are no break statements, no case keywords, and they are just generally much more compact–but also seem to be much more readable. Every line is in the form, “If it is this thing, then here’s the value to use.”

It is important to point out that the thing on the right side of the => is an expression that will be evaluated. We happen to be using literal values over there, but it could be any expression (as long as all the expressions are of the same type).

tip

I tend to prefer switch expressions over switch statements when possible. They are just so much cleaner and easy to use. If I can use a switch expression by rearranging my code a bit, I’ll usually do that.

info

Switches are actually far more powerful than these examples show. They’re the beginning point for pattern matching. We won’t dive into pattern matching here–it’s not the simplest of topics–but I’ll just point out that there’s a lot you can do with switches.

note

Everything you can do with a switch you can also do with an if statement. Not everything that can be done with an if statement can be done with a switch. An if statement allows for any arbitrary logic, while switches are more limited in Keep this in mind. Pick the tool that makes for the most understandable and obvious code.