C#9 new AND, OR, NOT pattern matching keywords

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.

C# 9 pattern matchingWill you use this new syntax?

Leave a Reply

Your email address will not be published. Required fields are marked *