Rider Help

Code Inspection: Merge sequential checks in && or || expressions

If you do a null comparison or similar checks, e.g. use HasValue, in the left part of a conditional-AND (&&) or a conditional-OR (||) operation, and then make some other check with this symbol or its members in the right part, Rider often suggests to simplify this operation by merging the two sequential checks. In most cases, the null-conditional operator (?.), introduced in C# 6.0, helps to do this.

Here are some examples of transformations that Rider suggests:

  • if (p == null || p.Arguments == null) > if (p?.Arguments == null)
  • if (t != null && t.Value is TimeSpan) > if (t?.Value is TimeSpan)
  • if (arg != null && arg is IConvertible) > if (arg is IConvertible)
Last modified: 11 October 2017

See Also