1

Possible Duplicate:
DateTime “null” value

There are many built-in classes in C# that are not Nullable. For example, Datetime, sometimes I would like to check if my datatime object is null or not but i am not allowed. How can you insert nullability to datetime class ?

Community
  • 1
  • 1

7 Answers7

4

There are many built-in classes in C# that are not Nullable.

They (Datetime) are structs which are value types, not class thus non-nullable.

MSDN - Value Types

Unlike reference types, a value type cannot contain the null value. However, the nullable types feature does allow for value types to be assigned to null.

You can use Nullable<T> with DateTime to make it nullable.

DateTime? dateTimeObject = null;

The question mark ? with DateTime makes it Nullable.

The syntax T? is shorthand for Nullable, where T is a value type. The two forms are interchangeable.

Habib
  • 219,104
  • 29
  • 407
  • 436
4

In short, Structs are value types. Value types must always be valid (hence why you have to initialise all members in a struct constructor) and can't be null.

There is a cheat however;

You can use Nullable<T>, which can otherwise be used as "T?" to wrap up the value type in a reference which can be nullable.

Usage:

DateTime? foo = new DateTime(...);

foo.Value.<datetime member>
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Immortal Blue
  • 1,691
  • 13
  • 27
2

it's enough to use ?, like

DateTime? dt = null;

So your non nullable type becomes Nullable Type

Tigran
  • 61,654
  • 8
  • 86
  • 123
2

You can add the character ? at the end of the type:

DateTime? date;
Bruno Croys Felthes
  • 1,183
  • 8
  • 27
2

Sure you can make an integer, double, datetime, etc. nullable with a change on the declaration.

DateTime? dt = null.

BUT

There are two things you need to be aware of.

1) DateTime? and DateTime are not the same data type so, if you do something like this:

DateTime? date1 = null;
date1 = DateTime.Now;

DateTime date2 = date1; // Wont work.

The correct way of accesing the value of your nullable type is like this:

DateTime date2 = date1.Value;

2) The second BUT is that you need to ALWAYS check if there is a value before you can use it.

DateTime? date1 = null;
DateTime date2 = date1.Value;

This will throw an exception, "Nullable object must have a value".

So it's not that simple as adding the nullable operator, you need to check the property "HasValue" every time you want to use your variable:

DateTime? date1 = DateTime.Now;
if(date1.HasValue)
{
    DateTime date2 = date1.Value;
}

Use nullables wisely.

Adrian Salazar
  • 5,279
  • 34
  • 51
  • Instead of using `HasValue`, I think `!= null` is more understandable. – svick Jan 30 '13 at 11:28
  • 1
    @svick: That's a matter of preference. I also use `HasValue` when it is a nullable type. – Daniel Hilgarth Jan 30 '13 at 11:29
  • Well, someting.HasValue is more semantic, that's the reason why Microsoft put this there in the first place. – Adrian Salazar Jan 30 '13 at 11:29
  • @AdrianSalazar I think at least one reason why `HasValue` exists is so that you can use `Nullable` in languages that don't have special support for it (like CIL). So, “they wouldn't put it there otherwise” is not a good argument. – svick Jan 30 '13 at 11:35
  • You can also use the `??` operator. If `date1` is nullable as above, you can say `DateTime date2 = date1 ?? new DateTime(2001, 1, 1);`. The value after the `??` operator is used as a kind of "fall-back" and is used only if `date1` does not `HasValue`. – Jeppe Stig Nielsen Jan 30 '13 at 11:36
  • @svick Well, this thing.HasSomething -> thing.Something is a pattern, and it's semantically correct. In this case you are not accessing "thing", you are accessing thing.Value. And you will ALWAYS have to go through thing.Value. Is this argument good enough now? – Adrian Salazar Jan 30 '13 at 11:47
1

Strictly speaking, DateTime is a struct (i.e. value-types), not a class (which are reference-types), which is why it is not nullable. Nullable<DateTime>, aka DateTime? is nullable, however. Nullable<T> is struct that has special handling by the compiler, making it work pretty-much like you expect in terms of ==, != (via .HasValue), and most other operations (via so-called "lifted operators").

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

DateTime is a struct, so it is value types. Value types can't be null. Unless you can use nullable types for DateTime or other value types;

Nullable types are instances of the System.Nullable struct. A nullable type can represent the correct range of values for its underlying value type, plus an additional null value.

Nullable types represents with question mark ?. For example;

DateTime? date = null;

DateTime? is equavalent to System.Nullable<DateTime> by the way.

If you use nullable DateTime, you can check it has a value or not like;

if(date.HasValue == true)

And of course you can use them with equality operators like == and !=

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364