JetBrains Rider 2024.1 Help

Code Inspection: Merge conditional ?: expression into conditional access

Category

Language Usage Opportunities

ID

MergeConditionalExpression

EditorConfig

resharper_merge_conditional_expression_highlighting

Default severity

Suggestion

Language

C#

Requires SWA

No

Starting from C# 6.0, you can use the null-conditional operator (?.) to test for null before performing member access.

If you use the conditional ?: (ternary) operator for nullability check when accessing symbol's members, JetBrains Rider suggests replacing it with a more elegant ?. operator.

Here is an example of a quick-fix suggested by this inspection:

string GetAttr(XElement node, string attrName) { var attrNode = node.Attribute(attrName); return attrNode == null ? null : attrNode.Value; }
string GetAttr(XElement node, string attrName) { var attrNode = node.Attribute(attrName); return attrNode?.Value; }
Last modified: 10 April 2024