switch case syntax specifies a data type
c#, c#-7.0, syntax
Solution
This is a new C# 7 feature: pattern matching switch statement which matches on types.
What this code does, giving the first branch:
- It checks if `someObject` is, derives from or implements type `TypeOne`.
- If so, it casts `someObject` to type `TypeOne` which is assigned to `valueOne`.
- Then it enters the case block where you can directly use `valueOne`.
Problem
Today at work I we did a code review as we do now and again, but today I saw a syntax I haven't seen before. I have searched the web for it to no avail, and what is even more interesting is that, despite using the same version of Visual Studio, that is, 2017 Enterprise, I get a syntax error at home when I try to recreate what I saw at work today. It looked something like this: ``` switch (someObject) { case TypeOne valueOne: action1(); break; case TypeTwo valueTwo: action2(); break; // ... and so on } ``` In other words, it looked like they checked both type and value in one go. But like I said, I can't find it on the internet and I get a syntax error at home. I know there's a lot of new stuff in C# 7, especially in terms of syntactic sugar. Can you explain this?