Nullable Types in Depth

This Article explains the Nullable Types in .NET 2.0

Nullable Types are instances of the System.Nullable struct introduced in .NET 2.0. Null means value undefined or value not known.

Reference types by design support storing null but values types have been designed to store only the range of values they are made for. They don’t have capacity to store null.

But many times there may be situation where we don’t know the value to store in value type e.g. in case of database fields where some fields may be empty of null.

To support such requirements Nullable types have been introduced.

Nullable types are supported using a Nullable Generic Structure in System Namespace.

public struct Nullable<T> where T : struct

The following struct can be used in following two ways :

Nullable<int> x = new Nullable<int> (100);

or

int? x = 100;

The two main properties of this struct are HasValue and Value.

HasValue indicates whether the current instance has a value. True if the instance has a value & false if has no value. The value in the object can be accessed by property Value.

The operator ?? can be used to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type.

e.g. int? x= null; int y = x ?? 1; y will be assigned a value of 1.

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)

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:

Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)
The syntax T? (in C#) is shorthand for System.Nullable<T>, where T is a value type. The two forms are interchangeable.

Assign a value to a nullable type in the same way as for an ordinary value type, for example:

C#: int? x = 10; or double? d = 4.108;

VB.NET: Dim x As Nullable(Of Integer) = 10 or Dim d As Nullable(Of Double) = 4.108
Use the System.Nullable.GetValueOrDefault method to return either the assigned value, or the default value for the underlying type if the value is null, for example

C#: int j = x.GetValueOrDefault();

VB.NET: Dim j as Integer = x.GetValueOrDefault()
Use the HasValue and Value read-only properties to test for null and retrieve the value.
The HasValue property returns true if the variable contains a value, or false if it is null.
The Value property returns a value if one is assigned, otherwise a System.InvalidOperationException is thrown.
The default value for a nullable type variable sets HasValue to false. The Value is undefined.
Use the ?? (C#) operator to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;
Nested nullable types are not allowed. The following line will not compile:

C#: Nullable<Nullable<int>> n;

VB.NET: Dim n as Nullable(Of Nullable(Of int))