tags:

views:

6269

answers:

9

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?

+32  A: 

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:

DateTime? MyNullableDate;

Or the longer form:

Nullable<DateTime> MyNullableDate;
Joel Coehoorn
It would greatly help if you provide an example of how to use it. How do you assign the DateTime in the database, which could be DBNull, to the nullable DateTime?
kirk.burleson
+3  A: 

I'd consider using a nullable types.

DateTime? myDate instead of DateTime myDate;

David Mohundro
+12  A: 

If you're using .NET 2.0 you can use the nullable type:

DateTime? dt = null;

or

Nullable<DateTime> dt = null;

then later:

dt = new DateTime();

And you can check the value with:

if (dt.ContainsValue)
{
  // Do something with dt.Value
}

Or you can use it like:

DateTime dt2 = dt ?? DateTime.MinValue;

You can read more here:
http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

Mark Ingram
You can use nullable types even in earlier versions of .NET, no need for 3.0.
stephenbayer
Typo, updated that.
Mark Ingram
It came in .NET 2.0 right? The ? syntax was added to VB.NET in 3.5, but it has been in C# since 2.0 I believe.
David Mohundro
Nullable types are available in .Net 2.0. C# has had the shorthand ? notation from 2.0. Only VB.Net didn't have the shorthand ? in 2.0 but you could use Nullable(Of DateTime)
Mendelt
For the last snippet, I would say DateTime dt2 = dt ?? DateTime.MinValue;
Joel Coehoorn
Thanks Joel, updated accordingly.
Mark Ingram
+1  A: 

You can use a nullable class.

DateTime? date = new DateTime?();
Aaron Smith
+3  A: 

You can use a nullable DateTime for this.

Nullable<DateTime> myDateTime;

or the same thing written like this:

DateTime? myDateTime;
Patrik
A: 

I always set the time to DateTime.MinValue. This way I do not get any NullErrorException and I can compare it to a date that I know isn't set.

Daok
That means you can't tell the difference between "I really need a DateTime here" and "It's optional" - Nullable<DateTime> is a much better solution, IMO.
Jon Skeet
? I really do not get your comment. Yeah I know when the DateTime is so far aways of reality is like if it was null...
Daok
I am just suggesting an other solution. Instead of repeating what have been suggested already 10 times here.
Daok
I mean that your type system can't indicate the optional nature of the value.
Jon Skeet
It is convenient, but I view DateTime.MinValue as a value, not a special case condition. It can only lead to problems down the line. I'd go with Nullable<DateTime>.
spoulson
Never got any problem since 2 years with this system.
Daok
There are any number of bad designs which won't always cause problems - but shouldn't be recommended anyway. I could come along and say that I make all members of all types public, and haven't had any problems - would that be good advice?
Jon Skeet
I think you miss something about my answer, Spoulson wrote something and I was answering. About public members, well it's not the same and you know it. it's like String.Empty or null. You can do both, not because String.Empty is better that null is wrong. What ever.
Daok
I'm with Daok on this. It might not be the "by the book" approach but it's better than getting a NullReferenceException or dealing with the cumbersome Nullable type. Besides, how many applications are out there that actually need to reference the date January 1, 1 A.D.?
Repo Man
+1  A: 

You can set the DateTime to Nullable. By default DateTime is not nullable. You can make it nullable in a couple of ways. Using a question mark after the type DateTime? myTime or using the generic style Nullable. I have added a couple of links on msdn.

Using Nullable

Nullable

A: 

use DateTime?.

A: 
            myclass.MyNullableDateTime = (drOrder["Field1"] == DBNull.Value) ? (DateTime?)null : ((DateTime)drOrder["Field1"]);
imanabidi