Nullable Datetime, DateTime Null value check

Value Type and Reference Type

A Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data.

All Reference Types are nullable by default, e.g. String, and all ValueTypes are not, e.g. Int32. The Nullable < T > structure is using a value type as a nullable type.

By default DateTime is not nullable because it is a Value Type, using the nullable operator introduced in C# 2, you can achieve this.

Using a question mark (?) after the type or using the generic style Nullable.

How to check the value of DateTime as null?

You can solve this problem in two ways, either make it a nullable type, or use the System.DateTime.MinValue.

DateTime datetime = new DateTime();

if (datetime == DateTime.MinValue)
{
    //unassigned
}
DateTime? datetime = null;

 if (!datetime.HasValue)
 {
     //unassigned
 }

Add Comment