When getting SQL DateTime Resharper suggests to use new DateTime()
when value is DBNull.Value
. I've always used DateTime.MinValue
. Which is the proper way?
DateTime varData = sqlQueryResult["Data"] is DateTime ? (DateTime) sqlQueryResult["Data"] : new DateTime();
The value of this constant is equivalent to 00:00:00.0000000 UTC, January 1, 0001, in the Gregorian calendar. MinValue defines the date and time that is assigned to an uninitialized DateTime variable.
The default and the lowest value of a DateTime object is January 1, 0001 00:00:00 (midnight). The maximum value can be December 31, 9999 11:59:59 P.M. Use different constructors of the DateTime struct to assign an initial value to a DateTime object.
You can do this immediately with LINQ using Enumerable. Min . DateTime minDate = dateCollection. Min();
Using the DateTime nullable type, you can assign the null literal to the DateTime type.
From the documentation of DateTime.MinValue:
MinValue defines the date and time that is assigned to an uninitialized DateTime variable.
Thus, the resulting date will be the same. Since DateTime
is a value type, both options should be equivalent. Personally, I prefer to write DateTime.MinValue
, since it's self-documenting.
PS: You might want to consider using nullable types (DateTime?
), if your data can contain (meaningful) null values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With