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: Possible 'System.InvalidOperationException'

Last modified: 08 April 2024

Before you cast a variable of a nullable type to its underlying type, you should make sure that the value is not null. In the example below, we have a nullable double x, and the expression (double)x will throw an exception if x is null.

ReSharper suggests checking for null:

Alternatively, ReSharper can add the assertion that the expression is not null: