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?

Using C# indices and range syntax to access elements in a sequence

Microsofts Indices and Ranges page provides examples of the indices and range syntax available in C# (introduced in C#8). This syntax provides a succinct way to access single elements or ranges in a sequence.

The first example below shows the ‘index from the end’ operator ^, which gives us a shorthand way of getting items relative to the end of a collection. Instead of using collection[collection.Length – 1] to get the last item we can just use ^1.

The second example below shows how we can specify a range of items to slice out from an array (note – slicing like this doesn’t work on Lists<>). Note that when using ranges the upper boundary is exclusive.

C# indices and range syntax

Using ToLower() or ToUpper() in C# to compare strings is not safe in all cultures

If you’re using Resharper, Roslynator or similar you’ll likely see string comparisons using ToLower() or ToUpper() flagged.


Well these comparisons are not safe in all cultures. The canonical example is the ‘Turkish i problem‘ which relates to how in Turkish the dot is present on the top of the uppercase version of ‘i’ and there is no dot on the lowercase version of the ‘i’. This means using .ToLower() etc. may return different results on machines with different culture settings.

On their Best practices for comparing strings in .NET page Microsoft recommends using StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.

StringCompare example in C#

Aside from safe comparisons, StringComparison also expresses intent more clearly and does not need to create strings so there’s a potential performance boost too.

Don’t use the C# var keyword everywhere in your code just to be consistent

Don’t use the var keyword everywhere in your code base just to be consistent. If using var removes clarity for certain assignments its best not to use it in these cases.

Example 1 below shows some usages of var in which the type is very clear from the right hand side. In these cases var is a nice addition as it improves readability as the variable names are all aligned and the type is not repeated.

On the other hand example 2 shows how var can be misused and ends up actually removing clarity as its not clear what the type is.

Var keyword examplesOf course when the type is not clear we can always mouse over the variable name and VS intellisense will tell us the type. This however is extra work and intellisense is not available in all contexts such as when viewing code online on GitHub etc.

Looks like its from 2015 but here are some Microsoft recommendations on implicitly typed variables.

Easier immutability with Init only properties in C# 9

C# 9 shipped with .NET 5 in November. One change included is init only properties which allow us to have immutable (non-changeable) classes without some of the downsides associated with pre C# 9 approaches.

As can be seen below in C# 8 we’d typically have getters with no setters which gives us immutability…great.. but it means we need a constructor and we can’t use the more shorthand object initializer (C# 3.0) syntax which many developers prefer.

In C# 9 we can use the init keyword which means we can get rid of the constructor and use shorthand object initialization.

This is a small but nice addition to the language and means developers don’t have to choose between ease of use and immutability.


C# 9 Language Features | On .NET | Channel 9 (msdn.com)

What’s new in C# 9.0 – C# Guide | Microsoft Docs