C# Nullable

In C#, some variable types are not null, e.g. int, bool. If the variable defined as null, will cause compile error. Nullable type means that the variable can be null. Nullable types can be represented by T? or System.Nullable.

int i = null; //compile error
int? j = null;
System.Nullable j = null;
bool? b = null;

Nullable type can be converted to common types.
int i = 3;
int? k = i;

Common types can be converted to Nullable types.
int? i = 3;
int k = (int)i;

To check whether a Nullable type variable is null or not:
int? i=3;
//if i=null, i.HasValue ==false;
if (i.HasValue == true) //i.HasValue == true
{
...
}



endmemo.com © 2017  Terms of Use | Home