Is there an easy way within C# to check to see if a DateTime instance has been assigned a value or not?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The only way of having a variable which hasn’t been assigned a value in C# is for it to be a local variable – in which case at compile-time you can tell that it isn’t definitely assigned by trying to read from it 🙂
I suspect you really want
Nullable<DateTime>(orDateTime?with the C# syntactic sugar) – make itnullto start with and then assign a normalDateTimevalue (which will be converted appropriately). Then you can just compare withnull(or use theHasValueproperty) to see whether a ‘real’ value has been set.