ReSharper 2017.1 Help

Code Inspection: 'if-return' statement can be re-written as 'return' statement

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 statement that returns control, immediately followed by another return, it suggests to replace those with a single return that makes use of the ?: operator.

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

Suboptimal codeAfter the quick-fix
string TryGetEntry(Dictionary<string, string> dict, string entry) { if (dict.ContainsKey(entry)) return dict[entry]; return entry; }
string TryGetEntry(Dictionary<string, string> dict, string entry) { return dict.ContainsKey(entry) ? dict[entry] : entry; }
Last modified: 12 October 2017

See Also