How to check for default DateTime value?
Solution 1:
I would probably make this really explicit:
// Or private, or maybe even public
internal static readonly DateTime MagicValueForNullDateTimeInNonNullableColumns
= DateTime.MinValue;
Okay, so I'd make the name slightly less wordy, but you get what I mean. It doesn't really matter much how you initialize that value - it's clear what you're trying to do.
(Of course, use a nullable type any time you can for this. I'm assuming the question was asked because you couldn't do so in certain situations.)
Solution 2:
I need to check a
DateTime
value if it has a value or not.
You've pretty-much described the textbook situation requiring a Nullable<DateTime>
. I'd go with that option.
(You'd obviously need some sort of translation layer if you need to persist the values to a non-null db column, but I'd try to keep any "magic" value as close to the db as possible, and try to keep your C# code clean and idiomatic.)
Solution 3:
The "best" practice would be using the nullable DateTime
. Nullable value types were created exactly for that reason of not trusting "magic" numbers.
What's most important about this approach is its intent - what does it mean in your context to have a "default" date/time?