csharp-condicional-switch

What is and how to use the conditional SWITCH in C#

  • 4 min

The switch conditional is a control structure in C# that provides a way to execute different blocks of code based on the value of an expression.

The switch conditional allows you to evaluate the value of an expression and execute specific code blocks based on different possible values of that expression.

If you want to learn more, check out the Introduction to Programming Course

It is sometimes used as a cleaner and more readable alternative to a series of nested if-else statements. Although, many people (myself included) think that instead of improving readability, it often worsens it.

Basic Syntax

The basic syntax of a switch conditional in C# is as follows:

switch (expression)
{
    case value1:
        // Code to execute if expression equals value1
        break;
    case value2:
        // Code to execute if expression equals value2
        break;
    ...
    default:
        // Code to execute if expression does not match any case
        break;
}
Copied!
  • expression: The expression whose value is evaluated in each case.
  • case: Represents a specific value that is compared with the expression.
  • default: Optionally, a default block can be included.

The default block will execute if none of the previous cases match the value of the expression (it is optional, but it’s most common to have it).

Let’s see a simple example where a switch conditional is used to print a number to the screen.

int number = 2;

switch (number)
{
    case 1:
        Console.WriteLine("One");
        break;
    case 2:
        Console.WriteLine("Two");
        break;
    case 3:
        Console.WriteLine("Three");
        break;
    default:
        Console.WriteLine("Invalid number");
        break;
}
Copied!

In this case, depending on the value of number

  • 1, 2, and 3 will print the corresponding number to the screen.
  • Any other number will show “Invalid number”.

Fall-Through Between Cases

Fall-Through is the possibility of “falling” from one case to another and executing multiple code statements. Unlike other languages (like C++), “fall-through” in C# is intentionally limited.

In C#, all blocks must

  • Either be empty
  • Or have a break statement

That is, it is not possible to do this, omitting the break in a case:

case 1:
    Console.WriteLine("One");
    // I cannot omit `break`
case 2:
    Console.WriteLine("Two");
// more cases
Copied!

It would be possible if the case statements are completely empty. Like this:

switch (number)
{
    case 1:
    case 2:
    case 3:      
        Console.WriteLine("Less than 3");
        break;
    default:
        Console.WriteLine("Greater than 3");
        break;
}
Copied!

Although it’s very likely a bad idea to do it 😉.

Practical Examples

These examples are intended to show how to use the Switch conditional. It does not mean it’s the best way to solve the problem they address. Usually, there are better alternatives.