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: 'if' statement can be rewritten as '?:' expression

Last modified: 08 April 2024

The conditional ?: (ternary) operator in C# is the most elegant way to execute one of two simple expressions depending on the value of a simple boolean expression.

Therefore, if ReSharper encounters an if...else statement in this context, it suggests converting it to a more succinct ?: operator.

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