Can anyone tell me why this gives an error at run-time:
Dim mightBeNothing As List(Of String) = Nothing
Dim a As List(Of String) = IIf(mightBeNothing Is Nothing, New List(Of String)(), New List(Of String)(mightBeNothing))
I am getting ArgumentNullException
on the second line. If I replace the last part with:
Dim a As List(Of String) = IIf(mightBeNothing Is Nothing, New List(Of String)(), New List(Of String)())
It works - but the constructor New List(Of String)(mightBeNothing)
will never be called if mightBeNothing
is nothing, so what is the issue?
the IIf function does not use short-circuit evaluation. So it will always evaluate everything, even if mightBeNothing is nothing.
MSDN on the subject.