C#9 (released Nov 2020) has some pattern matching enhancements. Added in C#9 (among other pattern changes) is conjunctive patterns (AND keyword), disjunctive patterns (OR keyword) and negative patterns (NOT keyword).
Two examples are below. These are simple examples of course but basically you don’t have to repeat the variable name as before and the syntax is a little more English like. I’ve seen a lot of developers say this new syntax reminds them of VB or Python.
Most likely if you’re checking against multiple strings as in example 2 you’d use a string array or collection with contains instead of this new syntax but I’m just showing what’s possible.
Will you use this new syntax?
Need Help with Your C# Projects?
We offer expert support and development services for projects of any size. Contact us for a free consultation and see how we can help you succeed.
CONTACT US NOW