Switch Statement Guide for C#

Article summary

You can use a variety of syntaxes in C#. You might assume you can use them interchangeably, but this is not the case. There are two main types of switch logic that can be used and two major types of matching.

Switch Statement or Switch Expression:

Statement

The type of switch that is commonly found in tutorials is a switch statement. They look like the following:


public int SwitchStatementExample()
{
   var fruit = "apple";


   var foo = 0;


   switch (fruit)
   {
       case "apple":
       {
           foo = 1;
           break;
       }
       case "orange":
       {
           foo = 2;
           break;
       }
   }


   return foo;
}

Here are a few things to notice. Each case can execute multiple lines of code up to the break or return. The condition for each case must also be a possible value of the variable provided to the switch. In the example, the switch statement will set foo to 1, break from the switch, and return foo. What happens when we want to have a case that is a conditional rather than just the value of the variable? Then we can use a switch expression.

Expression

Switch expressions use the same “switch” keyword, but, instead of executing arbitrary code in each case, they return the result of a statement. Pictured below is a simple switch expression.


public int SwitchExpressionExample()
{
   var fruit = "apple";


   var foo = fruit switch
   {
       "apple" => 1,
       "banana" => 2,
   };


   return foo;
}

The example is checking if the fruit is an apple, and if so, it returns 1 from the switch statement. The variable foo is set to the value returned from the switch and then returned from the function. This is effectively the same as the previous example. The main difference is that each case can only return a value from a statement or function call instead of executing an arbitrary number of lines. Now, it’s time to add the conditional that was mentioned previously.


public int SwitchExpressionExample()
{
   var fruit = "apple";


   var foo = fruit switch
   {
       _ when fruit == "apple" => 1,
       "banana" => 2,
   };


   return foo;
}

The _ when is now using C#’s pattern matching instead of matching on the variable value. The conditional fruit == “apple” can be replaced with an arbitrary Boolean or C# pattern. This opens up the possibility of checking for an array of values and, if the value is in the array, returning a specific value.

See this example for using contains to match an array of values.

 
public int SwitchExpressionExample()
{
   List vegetables = ["carrot", "broccoli"];


   var fruit = "apple";


   var foo = fruit switch
   {
       _ when fruit == "apple" => 1,
       "apple" => 1,
       "banana" => 2,
       _ when vegetables.Contains(fruit) => 3,
       _ => 0,
   };


   return foo;
}

 

With the given arguments, the .Contains case would never be hit. But, if the value of fruit was ever set to something not a fruit, we would fall into the Contains case and return 3.

The two forms of switches in C# have distinct strengths. Switch statements can execute a wider variety of actions after a case is met, but the cases must be relatively simple. Switch expressions have more flexible conditions, but they can only return a value or execute one function because they are lambda expressions.

Conversation

Join the conversation

Your email address will not be published. Required fields are marked *