All rules
IDE0078Language rules (pattern matching preferences)

Use pattern matching

Use pattern matching

Microsoft docs
Documented together with IDE0260Use pattern matching

Description

This style rule concerns the use of C# pattern matching constructs.

IDE0260 specifically flags the use of an as expression followed by a member read through the null-conditional operator. This rule is similar to IDE0019, which flags the use of an as expression followed by a null check.

Example

// csharp_style_prefer_pattern_matching = true
var x = i is default(int) or > (default(int));
var y = o is not C c;

// csharp_style_prefer_pattern_matching = false
var x = i == default || i > default(int);
var y = !(o is C c);

// Code with violations.
object? o = null;
if ((o as string)?.Length == 0)
{
}

// Fixed code (csharp_style_pattern_matching_over_as_with_null_check = true).
object? o = null;
if (o is string { Length: 0 })
{
}

Configurable options

Vote for the value each option should take in the generated .editorconfig.

csharp_style_prefer_pattern_matching
default: true
csharp_style_pattern_matching_over_as_with_null_check
default: true
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0