Nullable type and if issue

2020-02-14 05:50发布

问题:

Here is simplest piece of code

Dim testInvoiceDate As DateTime? = If(String.IsNullOrEmpty(Nothing),
                                      Nothing,
                                      New DateTime(2018, 3, 20))

Why variable testInvoiceDate is not Nothing , but #1/1/0001 12:00:00 AM# ?! That is very weird !

回答1:

The If-statement will return the same datatype for both cases.
Because the return-type in the False-case is DateTime, the return-type is the DateTime-default-value for the True-case.

Default for DateTime is DateTime.MinValue which is #1/1/0001 12:00:00 AM#.

This will work as expected:

Dim testInvoiceDate As DateTime? = If(String.IsNullOrEmpty(Nothing),
                                      Nothing,
                                      New DateTime?(New Date(2018, 3, 20)))


回答2:

That compiles in VB.NET(as opposed to C#) because here Nothing has multiple meanings.

  1. null
  2. default value for that type

In this case the compiler uses the second option since there is otherwise no implicit conversion between the DateTime and Nothing(in the meaning of null).

The default value of DateTime(a Structure which is a value type) is #1/1/0001 12:00:00 AM#

You could use this to get a Nullable(Of DateTime):

Dim testInvoiceDate As DateTime? = If(String.IsNullOrEmpty(Nothing), New Nullable(Of Date), New DateTime(2018, 3, 20))

or use an If:

Dim testInvoiceDate As DateTime? = Nothing
If Not String.IsNullOrEmpty(Nothing) Then testInvoiceDate = New DateTime(2018, 3, 20)


回答3:

It's because you're using the 3-argument form of If(). It will try to return the same type based on parameters 2 and 3, so the Nothing in parameter 2 gets converted to a DateTime (and you get DateTime.MinValue).

If you use the 2-argument form, it applies null-coalescing, i.e. when the first argument (which must be an Object or a nullable type) is Nothing, it returns the second argument, otherwise it returns the first argument.

If you use Dim foo As DateTime? = If(Nothing, new DateTime(2018, 3, 20)) you will get the expected value.



回答4:

Nothing in VB.Net is the equivalent of default(T) in C#: the default value for the given type.

  • For value types, this is essentially the equivalent of 'zero': 0 for Integer, False for Boolean, DateTime.MinValue for DateTime, ...
  • For reference types, it is the null value (a reference that refers to, well, nothing).

Asigning Nothing to a DateTime therefore is the same as assigning it DateTime.MinValue