Home > c# > Nullable Types

Nullable Types

Nullable types are special types of value type variables (including struct) which can accept the correct range of values for its underlying value type, plus an additional null value. For example, a Nullable<Int32> (pronounced “Nullable of Int32”) can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable<bool> can be assigned the values true/false, or null.

Nullable type variables can only be defined for value types and not for the reference types because reference types any way accept the null as valid value. Each nullable type is constructed from the >)>Nullable<T> structure where T is underlying variable type for which Nullable type to be created. Nullable types can be defined by appending ‘?’ to datatype text in declaration as shown below :

int? nullableInt = 4;

double? nullableDouble = 9.2;

Properties :

Nullable<T> structure has two read only properties defined namely ‘HasValue’ and ‘Value’.

HasValue : returns true/false depending upon the value stored inside the nullable variable. If value of the variable is null it returns false else it will return true.

Value : returns underlying value of the nullable variable if it is not null. If underlying value for the variable is null then it will throw “InvalidOperationException”.

Method :

Nullable<T> structure has one method named as ‘GetValueorDefault”.

GetValueorDefault : This method has got two overloads.

GetValueorDefault() : This method returns the underlying value of the variable if ‘HasValue’ is true else it will return the default value of underlying variable. e.g. when this method is called on nullable<int> and ‘HasValue’ is false then this method will return 0. If the underlying data type is Boolean then default value returned will be ‘false’.

GetValueorDefault(T) : In this method, default value is passed as an argument. So whenever ‘HasValue’ is false, it will return the passed value as default value.

Boxing and UnBoxing :

Nullable type variable is also an example of boxing and unboxing. When value is assigned to Nullable type variable, CLR boxes this value assigned to variable and stores it. It means, if ‘HasValue’ property is true, boxed value for the variable is present and if it is false, null is boxed. When nullable varaiable’s value is retrieved, CLR UnBox the previously Boxed value and return.

Explicit Conversion :

int? x = null;

int y = x; (This statement gives compile time error because conversion to int data type from Nullable<int> data type requires explicit conversion i.e. int y = (int)x. If during explicit conversion value of nullable variable’s (y in our case), value is null then it will throw exception. So it is better practices to use int x = y.GetValueorDefault();

Alternatively, to get the default value for the underlying variable type ?? can be used.

int? i = null;

int y = i?? – 1;

Return value for the y in above code will be -1 because as i is null and i?? will return 0 (default value for int data type).

Categories: c# Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: