Introduction

What made VB so famous was the expressiveness of the language. A program could be read almost like a plain English text. In comparison, C# and C++ were optimized for short syntax and performance. That choice made C# a fast but hard to read the language in some cases.

If Not a And b Then 
  ... 
Else 
  ... 
EndIf
if(!(a) && b)
{
...
}
else
{
}

It's just a simple example to make a point, but you can probably imagine that people are creating logical expressions that are way more complex than that and contain a lot of symbols.

Logical expression

C# 9.0 now supports a few new keywords like not, and, and or. It makes some expressions easier to read, and for the rest, you can still use the shorter syntax using the logical symbols. You now literally have the best of both worlds.

if(s is not string)
// is way more easier to read than
if(!(s is string))

Pattern matching

These new operators are useful when you want to combine them with pattern matching. Speaking of which, you can now also use relational patterns like >=, >, and so on, directly in pattern matching expressions.

Person p = new Person();
var a = p.Weight switch
{
  < 150 => "light"
  >= 150 and < 200 => "normal"
  not null => "unknown"
  null => "error"
};

Before C# 9.0, some patterns were impossible to represent. eg. not null. It was mainly a compiler's limitation because !null wasn't a valid parsable token by C#. Now with the introduction of the new logical keywords like not, that problem goes away. The same applies to expressions like >= 150 and < 200. Before C# 9, it wasn't a valid syntax either, as the compiler didn't know how to parse && followed by another logical symbol like <. In a pattern-matching expression, the compiler knows what variable we are matching on and infer what that expression means without the repetitive declaration of variables that only adds noise to the logical expression. In the end, we got a syntax that is much easier to understand for humans.

Bottom line

The introduction of the new logical keywords and, or, and not will unlock the possibilities to write logical expressions that were invalid before. It's another excellent addition that makes C# a more accessible language to learn and play with.

If you want to know more about C# 9.0, check out my getting started article.