I've been searching a lot but couldn't find a solution. How do you deal with a DateTime that should be able to contain an uninitialized value (equivalent to null)? I have a class which might have a DateTime property value set or not. I was thinking of initializing the property holder to DateTime.MinValue, which then could easily be checked. I guess this is a quite common question, how do you do that?
C# – DateTime “null” value
cdatetimenull
Related Topic
- Javascript – How to check for an empty/undefined/null string in JavaScript
- Mysql – Should I use the datetime or timestamp data type in MySQL
- Python – Converting string into datetime
- Sql-server – Altering a column: null to not null
- Javascript – How to determine if a variable is ‘undefined’ or ‘null’
- Java – Is null check needed before calling instanceof
- Python – Referring to the null object in Python
- Javascript – standard function to check for null, undefined, or blank variables in JavaScript
Best Answer
For normal DateTimes, if you don't initialize them at all then they will match
DateTime.MinValue
, because it is a value type rather than a reference type.You can also use a nullable DateTime, like this:
Or the longer form:
And, finally, there's a built in way to reference the default of any type. This returns
null
for reference types, but for our DateTime example it will return the same asDateTime.MinValue
:or, in more recent versions of C#,