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?
This is a new C# 7 feature: pattern matching switch statement which matches on types.
What this code does, giving the first branch:
someObject
is, derives from or implements type TypeOne
.someObject
to type TypeOne
which is assigned to valueOne
.valueOne
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With