Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to use DateTime.MinValue or a nullable DateTime?

Tags:

c#

datetime

If I had a DateTime on a class called "TimeLastAccessed", would it make more sense for this DateTime to be nullable:

public DateTime? TimeLastAccessed { get; set }

if (TimeLastAccessed == null) // ... handle it

to indicate it has never been accessed or check for DateTime.MinValue

public DateTime TimeLastAccessed { get; set; }

if (TimeLastAccessed == DateTime.MinValue) // ... handle it

?

like image 382
Kermit Avatar asked Jan 27 '10 03:01

Kermit


1 Answers

It makes more sense to use Nullable. That's the idea of Nullable - to express that a value type has no valid value. To use MinValue is a patch for cases you don't have Nullable.

like image 140
Ariel Avatar answered Sep 30 '22 18:09

Ariel