Language: EN

csharp-condicional-switch

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

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 evaluating the value of an expression and executing specific code blocks based on different possible values of that expression.

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

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;
}
  • 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 is most common to have it).

Let’s see it with a simple example where a switch conditional is used to print a number on 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;
}

In this case, depending on the value of number

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

Fall-Through between cases

Fall-Through is the ability to “fall” from one case to another, 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

It would be possible if the cases 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;
}

Although, it is very likely to be a bad idea to do so 😉.

Practical Examples

Determine the name of a number

In this example, we use the switch structure to determine the name of a number based on its value.

int number = 2;

// We use switch to determine the name of the number based on its value
switch (number)
{
    case 1:
        Console.WriteLine("One");
    case 2:
        Console.WriteLine("Two");
        break;
    case 3:
        Console.WriteLine("Three");
        break;
    default:
        Console.WriteLine("Invalid number");
        break;
}

Determine the mathematical operation based on the operator

This code shows how to use a switch to determine the mathematical operation based on the provided operator.

char operator = '+';

// We use switch to determine the mathematical operation based on the operator
switch (operator)
{
    case '+':
        Console.WriteLine("Addition");
        break;
    case '-':
        Console.WriteLine("Subtraction");
        break;
    case '*':
        Console.WriteLine("Multiplication");
        break;
    case '/':
        Console.WriteLine("Division");
        break;
    default:
        Console.WriteLine("Invalid operator");
        break;
}

Determine the name of a day of the week

This example shows how to determine the name of a day of the week using a switch structure.

int day = 3;

// We use switch to determine the name of the day based on its value
switch (day)
{
    case 1:
        Console.WriteLine("Monday");
        break;
    case 2:
        Console.WriteLine("Tuesday");
        break;
    case 3:
        Console.WriteLine("Wednesday");
        break;
    // We add cases for the remaining days...
    default:
        Console.WriteLine("Invalid day");
        break;
}

Determine the name of a month (with enumeration)

Here a switch is used to determine the name of a month based on its value in an enumeration.

// We define an enumeration for the months of the year
enum Months { January = 1, February, March, April, May, June, July, August, September, October, November, December }

// We declare a variable of the enumeration to represent the month
Months month = Months.May;

// We use switch to determine the name of the month based on its value in the enumeration
switch (month)
{
    case Months.January:
        Console.WriteLine("January");
        break;
    case Months.February:
        Console.WriteLine("February");
        break;
    // We add cases for the remaining months...
    default:
        Console.WriteLine("Invalid month");
        break;
}

These examples are meant to show how to use the Switch conditional. It does not mean that it is the best way to solve the problems they address. Usually, there are better alternatives.