1. 雖然IIF可簡化程式行數但效能不彰, 在循環測試下 iif = 0.002 sec, if = 0 sec
Dim _obj As Object = False
Dim result As Boolean
Dim _h As New HiResTimer
_h.StartTimer()
For i As Integer = 0 To 100
result = CType(IIf((TypeOf _obj Is System.Boolean), CType(_obj, Boolean), False), Boolean)
Next
_h.StopTimer()
Debug.WriteLine(_h.ElapsedTime)
_h.StartTimer()
For i As Integer = 0 To 100
If (TypeOf _obj Is System.Boolean) Then
result = CType(_obj, Boolean)
Else
result = False
End If
Next
_h.StopTimer()
Debug.WriteLine(_h.ElapsedTime)
2. IIF不適合作為型別檢查之判別
當 Dim _obj As Object = "ABC" 時取得評估為 False但仍會導致 CType(_obj, Boolean)發生型別轉換錯誤
因為雖然 IIF 所得為 FALSE 但仍會處理 TRUE的型別轉換因此導致錯誤發生
也因為無論TRUE或FALSE都會處理當中的 Expression 如此也失去了效能
3. IIF 所得TRUE或FALSE當中仍為 Expression 因此須注意返回值
result = CType(IIf((TypeOf _obj Is System.Boolean), result = CType(_obj, Boolean), result = False), Boolean)
所得的將會是 Expression (result = CType(_obj, Boolean))比對後返回結果