Nullable variable types - .value member

c#, member, nullable, types

Solution

The value property is read only and will return the actual value type. The value property can never be null.

If you expect to have a nullable return a value then check .HasValue and then reference Value. For instance, if you want to assign the value of a Nullable to an ordinary bool then you have to reference it's value:

bool? nullableBool = null;

if (nullableBool.HasValue)
{
    bool realBool = nullableBool.Value;
}

However, the following won't compile:

bool? nullableBool = true;
bool realBool = nullableBool; // Won't work

Problem

I was wondering - when would I want to use the .Value member on a nullable type instead of just calling the variable itself? e.g.. bool? b = true; why would i use b.Value to get the value instead of just using b? What advantage or function does the .Value call add?

Original source