ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: Merge sequential checks into single conditional access check

Last modified: 11 February 2024

If you do a null comparison or similar checks, for example 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, ReSharper often suggests simplifying 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 ReSharper 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)