Skip to main content

RCS1146: Use conditional access

Properties​

PropertyValue
Default SeverityInfo
Minimum language version6.0

Examples​

Example #1​

diagnostic.cs
if (s != null && s.StartsWith("a"))
{
}
fix.cs
if (s?.StartsWith("a") == true)
{
}

Example #2​

diagnostic.cs
if (dic != null && dic[0].StartsWith("a"))
{
}
fix.cs
if (dic?[0].StartsWith("a") == true)
{
}

Example #3​

diagnostic.cs
if (x != null)
x.Foo();
fix.cs
x?.Foo();

Configuration​

.editorconfig
# Do not suggest to use null-conditional operator when result would be `... != true/false`
# Default value is false
roslynator_null_conditional_operator.avoid_negative_boolean_comparison = true|false

Applies to​