Mind-Bending Null-Check in Comparison Evaluation Can Be Improved
I have just noticed that nullables used in a comparison can be further checked to eliminate a false warning:
Where `Value` is `double?`:
double? priorValue = Value;
Value = null;
// ... (Re-compute Value here)
return ((priorValue.HasValue != Value.HasValue)
|| (Value.HasValue
&& !CompareUnderTolerance(Value.Value, priorValue.Value));
Resharper warns that `priorValue.Value` might be null ... but it isn't.
Please sign in to leave a comment.
As a workaround, you could use:
If you check the source, the only difference between Value and GetValueOrDefault is that Value checks the HasValue field, and throws an exception if it's false. Since you already know that both variables have a value, there's no point in checking it again.
In a simpler example, ReSharper is wrong:
Snippet
Using GetValueOrDefault implies that "number" is valid within the "else" block.
Hello!
Thank you for the feedback.
Please feel free to comment or vote for correspondent feature request - https://youtrack.jetbrains.com/issue/RSRP-468111
Thank you.
As a workaround for the string interpolation, you can drop the ".Value" without changing the output.
string s = $"{number:#,##0}";