Since C# was first introduced, the switch statement has been an integral part of it. If you're a developer who works with this language, or even just someone who's interested in it, you may have noticed that there was a lack of flexibility in what we could do with the C# switch. But not anymore!
In the last couple of versions of C#, Microsoft has made enhancements related to the switch statements and if you keep reading, you'll see how the switch constructs are getting better and better, allowing you to write cleaner and easier-to-read code.
Ready to get started?
What are Switch Statements?
Switch statement is a selection control tool which allows a variable to be tested for equality. It is tested against other values.
An old school way of coding the C# switch
Suppose you need to write a method that returns a job candidate's skill level based on their years of experience and you're asked to label their experience level according to the following rules:
- 0 years of experience: Inexperienced
- 1 or 2 years of experience: Beginner
- 3-5 years of experience: Intermediate
- More than 5 years of experience: Expert
Here's one of the possible solutions using the old switch statement (C# 6 and before):
The method above uses a switch statement to check the value of the yearsOfExperience variable passed to it. Then, it returns a string value representing the job applicant's experience level. Notice that in a switch statement, you have to specify each case label. Also, don't forget to add the break keyword where you need to jump out of the conditional statement.
All values that are over 5 will fall into the Expert category. But because there are too many, we put it in the default case.Overall, the code looks simple and easy to read but very inflexible for checking a range of values.
Let's see an example. Suppose you want to add checking for negative values within the switch statement above. In this case, adding a case label for every negative value is clearly not a good solution. You may want to convert the switch-case to an if-else block instead.
C# 7: switch statement using range operators and when clauses
Microsoft released C# 7.0 in 2017. And one of its evolutionary features related to the switch statement is that it lets you specify a condition in a case block with the when keyword.
Here's an example of how the code looks cleaner in C# 7:
With C# 7, you can use range operators within a case statement. The structure of the above code looks cleaner than the old switch statement. More importantly, it's powerful for handling a range of values. But wait! There's more...
C# 8: switch expressions
At a glance, you can tell that the following code is more compact compared to the previous one:
Notice that the switch keyword appears after the yearsOfExperience variable. Each case expression ends with a comma. And one more interesting thing here is that you won't find any case or break keyword in the expression.
The switch expressions are a powerful feature of C# 8. And it allows you to write significantly fewer lines of code compared to C# 7.
In fact, you can still simplify the above code! By returning the result of the expression directly and turning the entire block into an expression-bodied method, you'll get the following code—which is shorter:
When it comes to code, less is more. However, you don't have to go for the most minimalistic brilliant line of code you can come up with.
Sometimes, it's better not to go overboard with your code. Keep in mind that readability matters, especially if you're working on a team, where other people need to be able to understand the code you've written.
C# 9: switch expressions with pattern matching
As of this writing, the latest version of C# is version 9.0.
There are many updates in this version, including enhancements in pattern matching, which allows you to use the <, >, <=, and >= operators in a switch expression in a more natural way:
Notice that the above code does not use any 'when' keywords. The patterns before the => symbol are more straightforward, easier to read than the previous code.
Hit the switch...what's the conclusion?
So we've gone through the different C# switch constructs, from the old switch statement to switch expression using pattern matching in the latest versions of C#. Each newer version of the language has better features and allows you to write cleaner switch code than the previous version, so we hope you can take these new iterations on board when next writing your code.
Being able to write code that is clean and easy to read demonstrates your good qualities as a programmer. If you're interviewing as a C# developer, understanding how to code the switch expressions using the latest features is definitely a good advantage to have. And if you're looking for a new tech role using C#, we've got you covered, sign up to hackajob here and you could be starting your next job in just weeks.