Here is simplest piece of code
Dim testInvoiceDate As DateTime? = If(String.IsNullOrEmpty(Nothing),
Nothing,
That compiles in VB.NET(as opposed to C#) because here Nothing has multiple meanings.
null 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)
Nothing in VB.Net is the equivalent of default(T) in C#: the default value for the given type.
0 for Integer, False for Boolean, DateTime.MinValue for DateTime, ... null value (a reference that refers to, well, nothing).Asigning Nothing to a DateTime therefore is the same as assigning it DateTime.MinValue
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.
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)))