Looking for a better way to compare a nullable date time than the following:
Any suggestions?
// myobject.ExpireDatetime is of DateTime?
//
if (!myobject.ExpireDateTime.IsNull() && DateTime.Compare((DateTime)myobject.ExpireDateTime, DateTime.Now.ToUniversalTime()) < 0)
{ //error! }
Edited: Sorry for confusion…myobject.ExpireDatetime is of type
DateTime.
Your question is not quite clear to me, but if we have
it’s OK to say just
This will be OK if
ExpireDateTimeisnull(HasValueis false). Some inexperienced developers will struggle to understand lifted operators, so to make it more clear, you could writeIt’s the same, but easier to read and understand.
Never write
.Valueif the nullable might be null, of course. You will get anInvalidOperationException“Nullable object must have a value” if you do so.