Those of us who've worked in VB/VB.NET have seen code similar to this abomination:
Dim name As String = IIf(obj Is Nothing, "", obj.Name)
I say "abomination" for three simple reasons:
IIf
is a function, all of whose parameters are evaluated; hence if obj
is nothing in the above call then a NullReferenceException
will be thrown. This is unexpected behavior for someone who's accustomed to short-circuited ternary operators in languages like C#.IIf
is a function, it thus incurs the overhead of a function call. Again, though this isn't a big deal, it just doesn't feel right to someone who expects for it to behave as a ternary operation intrinsic to the language.IIf
is non-generic and therefore accepts parameters of type Object
, which means the following call boxes (I believe) a total of three integers:
' boxes 2nd and 3rd arguments as well as return value '
Dim value As Integer = IIf(condition, 1, -1)
Now, in some more recent version of VB.NET (I'm not sure what the number is), the If
operator was introduced, which works exactly the same way as the IIf
function but (as I understand it) without the same shortcomings. That is to say, it does provide short-circuiting and it is an intrinstic VB operation. However, I'm not sure about the last part. The MSDN documentation doesn't seem to indicate whether If
boxes its arguments or not. Does anyone know?
The main thing is that you correctly identified the new If
as an operator rather than a function. It is also typesafe and therefore does not need boxing, and is a direct mapping to the conditional/ternary/? operator in C/C++/C#/Java/etc
Even without the new operator, you can get some improvement in VB.Net with this code:
Public Shared Function IIf(Of T)(ByVal Expression As Boolean, ByVal TruePart As T, ByVal FalsePart As T) As T
If Expression Then Return TruePart Else Return FalsePart
End Function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With