Nullable Types

Nullable types can represent all the values of an underlying type plus an additional null value.


System.Nullable<T> variable; 
T? variable;

T can be any value type, including struct
T cannot be a referenced type


Every nullable type has two public readonly properties
HasValue - false when variable contains a null value
Value - the value when variable does not contain a null value



Boxing

Objects based on nullable types are only boxed when the object is not null.

bool? B = null; 
object o = b;
//o is null

bool? B = true;
object o = b;
//o is true

if (b == null)
{
}




© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext