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: Conditional expression can be rewritten as null-coalescing

Last modified: 23 September 2024

If you want to assign a value, pass an argument, or return from a method based on the nullability of an identifier, the clearest syntax you can use in these cases is the ?? (null-coalescing) operator.

Therefore, whenever ReSharper encounters a conditional ?: (ternary) operator in the above-mentioned contexts, it suggests simplifying the expressions using the ?? operator.

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

Speaking about the performance of both operators, there is no observable difference. Actually, the ?? is even a tiny bit faster.