The switch statement in C# was completely inherited from the C programming language and it has always been very verbose.

C# 8 introduces a new streamlined way to write conditional code as an expression.

Switch Expression in C# 8

The switch expression has some limitations (by the fact that is an expression and must always return a value) so you can’t replace all usages of switch statements in your projects. However, in most cases, the replacement can be done, and I would recommend you to always use the new syntax over the old one when possible.

The switch expression also support a new set of patterns like property patterns, tuple patterns and positional patterns that help you write complex pattern matching scenarios in a few lines of code.

5y
11.9k
500
  • 3
Microsoft MVP, master certified C# developer and founder of .NET Cambridge: the official community on .NET in Cambridge (https://www.productivecsharp.com). I love to teach in a simple and practical way how to master the C# language and help you to become one of the most productive C# developer in the world. I am a Senior .NET Software Engineer working in Redgate Software. My peers describe me as really enthusiastic, motivated with an infinite passion for software development, a relentless desire to keep learning and an amazing ability to involve others in my pursuit of excellence.