Wednesday, August 27, 2008

Nullable type in .Net 2.0

Nullable type allows a varible to store null value, Infact this is the special about the type. The main purpose is it will add two members to the variable HasValue and Value Members.
for example, if you store data for a yes/no question and if the user did not answer the question then null will be stored in that place. This means it stores True, False and third state nothing as null also. This will be represented as shown below.

In VB.Net,
Dim b as Nullable(Of Boolean) = Nothing
.....
If b.HasValue Then
Do if b has some value and not null
Else
Do if b is null
End If
In C#,
Nullable b = null;

Otherwise it can be used this way also, only applicable for C#,
bool? b = null;
.....
if (b.HasValue) {Do if it is not null} else {Do if it null}

No comments: