Thursday 5 June 2014

C#.NET - How to assign null value to DateTime / Working with nullable DateTime / Assigning null value to DateTime / Setting DateTime to null

Value types are non nullable. Reference types are nullable.

DateTime is a value type. If you try to assign null value to DateTime it will throw compile time error "Cannot convert null to System.DateTime because it is a non-nullable value type."
assign null value to DateTime
DateTime has the default value that is equal to the DateTime.MinValue. You can assign this to DateTime.
Assigning default value to DateTime
In C# 2.0, Microsoft introduced "Nullable Types" using the nullable operator (?). It will allow you to assign the null values to the value types. So we can assgin a null value to the DateTime by making it as Nullable Type.
Assigning null value to DateTime