Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
| Property | Value | 
|---|---|
| Rule ID | IDE0031 | 
| Title | Use null propagation | 
| Category | Style | 
| Subcategory | Language rules (expression-level preferences) | 
| Applicable languages | C# and Visual Basic | 
| Options | dotnet_style_null_propagation | 
Overview
This style rule concerns the use of the null-conditional operator versus a ternary conditional expression with null check.
Options
Set the value of the associated option to specify whether null-conditional operators or ternary conditional expressions with null checks.
For more information about configuring options, see Option format.
dotnet_style_null_propagation
| Property | Value | Description | 
|---|---|---|
| Option name | dotnet_style_null_propagation | |
| Option values | true | Prefer to use null-conditional operator when possible | 
| false | Prefer to use ternary null checking where possible | |
| Default option value | true | 
// dotnet_style_null_propagation = true
var v = o?.ToString();
// dotnet_style_null_propagation = false
var v = o == null ? null : o.ToString(); // or
var v = o != null ? o.ToString() : null;
' dotnet_style_null_propagation = true
Dim v = o?.ToString()
' dotnet_style_null_propagation = false
Dim v = If(o Is Nothing, Nothing, o.ToString()) ' or
Dim v = If(o IsNot Nothing, o.ToString(), Nothing)
Suppress a warning
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable IDE0031
// The code that's violating the rule is on this line.
#pragma warning restore IDE0031
To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.IDE0031.severity = none
To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
For more information, see How to suppress code analysis warnings.