Introduction

Have you ever needed to add a null value to your integer, date & time,decimal or Boolean etc... data types?! I'm sure you may faced this desire before and you had to work around it to submit null value to those value data types. For example using DateTime.MinValue as a mark to a null value. But you cannot submit DateTime.MinValue to SQL Server Database, as this will throw an exception.

So .Net Framework 2.0 comes out with a great new feature and solution to your problem. It is The Nullable Value Types. I'm saying .Net Framework to generalize the feature as it is a new feature in both C# and VB.NET.

The New Story

In .Net Framework 2.0, value types can be extended to take either their normal values or a null value. Such an extension is called a nullable type.

Nullable type is constructed from the generic Nullable structure. By having this feature, you can submit null values to you database table field normally, like submitting a null DateTime value to a datetime field in your database table.

Now how can we declare a nullable value type?

Simply like this:

C#

DataTime? dtmVarName = null;
int? intVarName = null;
bool? bVarName = null;

VB.NET

Dim dtmVarName As Nullable(Of DateTime)
Dim intVarName As Nullable(Of Integer)
Dim bVarName As Nullable(Of Boolean)

How can you check if your nullable value type contains data?

Simply by checking HasValue property attached to your nullable value type

C#:

if (dtmVarName.HasValue)
{
     //...do something
}

VB.NET:

If dtmVarName.HasValue Then
     '...do something
End If

To extract your normal value from your nullable type, use Value property, only if HasValue returns true.

Conclusion

The following is written in MSDN, but I found it very useful to be used to close this article. Nullable types have the following characteristics: