Table of Contents

AL0014: Prefer pattern matching for null and zero comparisons

Pattern matching syntax (is/is not) is more expressive and idiomatic. For null checks, it also bypasses overloaded equality operators.

When it triggers

// AL0014: Use 'x is null' instead of 'x == null'
if (x == null) { }

// AL0014: Use 'x is not null' instead of 'x != null'
if (x != null) { }

// AL0014: Use 'count is 0' instead of 'count == 0'
if (count == 0) { }

Why this matters

For null checks:

  • x == null may invoke an overloaded == operator
  • x is null always performs a reference comparison
  • Pattern matching is the idiomatic C# way to check for null

For zero checks:

  • Pattern matching provides consistency with null checks
  • More readable and expressive

How to fix

Use pattern matching:

if (x is null) { }
if (x is not null) { }
if (count is 0) { }
if (count is not 0) { }

Configuration

[*.cs]
dotnet_diagnostic.AL0014.severity = suggestion