Evolution of switch in C#

Mihir Dave
4 min readDec 24, 2021

The Purpose

I was going through the new features of C# 10, that’s when realized how the switch has come a long way from “switch” in C# 6 or before, Back in the day switch cases were simple, but now it’s a feature-loaded powerful beast that we should use in our daily development, so that’s when I decided to write about switch’s journey from C# 6 to C#9.

C# 7

We will start with C# 7 because that’s when things started changing for the switch, So according to Microsoft

In C# 6 and earlier, you use the switch statement with the following limitations:

A match expression must be of one of the following types: char, string, bool, an integral numeric type, or an enum type.

Only constant expressions are allowed in case labels.

But starting with C# 7 we do not have this limitation, and now we can use pattern matching with switch cases, what does that mean? let’s see that in action.

So as you can see in the above example now we not only use other Types but it supports comparing types out of the box.

Note: localInput ‘s scope is only within the matching case block.

Another cool feature that comes with pattern matching is case guards , by definition, it is:

an additional condition that must be satisfied together with a matched pattern. A case guard must be a Boolean expression. You specify a case guard after the when keyword that follows a pattern, as the following example shows:

so we can use the “when” keyword along with the pattern, which adds another layer of the check before selecting a match. let’s see that using an example.

The above example shows how many possibilities we have with these new features, where we can use objects for matching patterns in switch statements.

C# 8

Now this version of C# came with a whole new exciting thing called “Switch Expressions” (which is different than switch statements).

In my opinion, one of the main purposes of switch expression was to remove unnecessary/repetitive code(case, break), let’s see switch expression in action.

Here is how you would normally write switch statements

But now we can convert the same in Switch Expressions

so there are few notable changes in the syntax of switch expression compared to switch statement
1. Variable comes before switch keyword (without brackets)
2. caseand defaultis replaced with =>and _
3. _ is used as catch-all same as the default of switch statement 4.

As we can see and IMO switch expression removes the hassle to write those extra repetitive lines of code and makes code more readable(subject to individual preference) and concise.

but that’s not all with switch expression C# 8 comes with various pattern matching enhancements which can be used along with switch expressions
like

  • Constant pattern
  • Discard pattern
  • Property pattern

we already saw the Constant and Discard patterns in the above example.
Let’s see an example of a property pattern.

As you can see we can use the property of a given object to construct our expression to match and all these things give a lot of flexibilities to switch cases opens up a lot of possibilities.

C# 9

In C# 9 we can see switch expressions capabilities being extended using new patterns being introduced like

  • Relational patterns
  • Logical patterns

Relational patterns allow you to use relational operator(<, >, <= or >=) in your expression to match the pattern

Logical patterns in C# 9 allow you to use one or more logical operators (not, and, and or) in your expression to match the pattern, like

Side note: one thing that I observed is we can use “or operator” to combine different expression which has only one action.

NOTE: in relational expression, there’s a constraint on the right-hand side of expression you can only use constant of type (integer, floating-point, char, or enum) still, an important and handy feature to have

Note: logical operators precedence should be kept in mind while using more than one operator and parentheses should be used whenever necessary.

Conclusion:

The Switchhas evolved a lot over time, and from the looks of it C# language design team is in no mood for stopping here, we can see more features being added to switch cases over time.

Also, this is another way to write some of the if, else if, else conditions using less cluttery code.

So this is a good time to start using switch(expressions/statements) in your development whenever and if you can(Assuming you are using C# 7 or above).

You can find all the code in the GitHub repo HERE.

Say Hi to Author😎:

StackOverflow

Linkedin

GitHub

--

--

Mihir Dave

Enthusiastic Senior Software Developer who loves C# contributes on StackOverflow and works on MongoDB|MsSql|MySql|Redis|Asp.Net MVC (WebAPI) C#|VueJS|Angular 6+