From the course: Ten Tips for the C# Developer

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Better switch statements with pattern matching

Better switch statements with pattern matching - C# Tutorial

From the course: Ten Tips for the C# Developer

Start my 1-month free trial

Better switch statements with pattern matching

- [Instructor] The switch statement has been part of the C# specifications since the beginning. It's been around for decades. It's always had this brittle syntax, at least when you compare it to other programming languages like Visual Basic. There's just less flexibility in what you could do in the C# switch. But that is changing. Microsoft has made some edits and updates in the last couple of versions of C# and I thought we would look at something called pattern matching in this video. We'll start by looking at the old style of a case and a switch. So here, I am switching on an integer value called number. That was one of the restrictions we saw in the past, is that there was only a few types you could switch on, some numeric types like integers and longs and some strings. That's now been improved and I'll show you that later in this video. And then when it came to writing your case labels, you tended to…

Contents