ReSharper 2016.1 Help

Code Inspection: Merge conditional ?: expression into conditional access

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

If you use the conditional ?: (ternary) operator for nullability check when accessing symbol's members, ReSharper suggests to replace it with more elegant ?. operator.

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

Suboptimal codeAfter the quick-fix
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; }

See Also

Last modified: 19 August 2016