Nullable double NaN comparison in C#

With all Nullable<T> instances, you first check the bool HasValue property, and then you can access the T Value property.

double? d = 0.0;        // Shorthand for Nullable<double>
if (d.HasValue && !Double.IsNaN(d.Value)) {
    double val = d.Value;

    // val is a non-null, non-NaN double.
}

You can also use

if (!Double.IsNaN(myDouble ?? 0.0))

The value in the inner-most parenthesis is either the myDouble (with its Nullable<> wrapping removed) if that is non-null, or just 0.0 if myDouble is null. Se ?? Operator (C#).

Tags:

C#

Double

Nan