‘Stupid’ Question 32: How do you check if the value for a DateTime property is set?
Can time be null? Not unless you say it can
Today I was looking for an alternative way to check if DateTime as been assigned a value or not, if I’m not using a nullable type. I was just curious if this could be done. The thing is, DateTime is a Value type,- so it can’t be null unless you of course make it a nullable type. Value types are stored as the value they are assigned, in contrast to reference types that point to an object - and thus it makes no sense having a value type with the value of null .
A nullable type lets you represent a value type as anything within the set range, or as null. Now, what if you, for whatever reason, don’t want to make a value a nullable type? What you can do is to use the DateTime.MinValue, which is the smallest possible value that a DateTime can have, and also the value an unassigned DateTime has.
I reckon that is makes more sense to make DateTime a nullable type, what do you think? When would be a good time to use DateTime.MinValue?
Last modified on 2012-08-26