2

Using vb.net the following code throws a null pointer exception but the expression should guarantee that match is not null.

repeater.DataSource = IIf(collection IsNot Nothing AndAlso match IsNot Nothing, collection.FindAll(match), collection)

Replacing this with regular if-else construct no error is thrown:

If collection IsNot Nothing AndAlso match IsNot Nothing Then
    repeater.DataSource = collection.FindAll(match)
Else
    repeater.DataSource = collection
End If

Are both path's evaluated in an ternary operator?

4

2 回答 2

3

If 运算符 (Visual Basic) - MSDN

使用三个参数调用的 If 运算符与 IIf 函数一样工作,只是它使用短路评估。IIf 函数总是计算它的所有三个参数

于 2013-09-05T13:48:06.093 回答
1

是的,双方都进行了评估,您应该避免使用IIf()语法并If()改用,因为If()会与AndAlso.

有关更多信息,请阅读接受的VB.NET 答案 - IIF(,,) - 评估“双方”。我应该注意哪些情况?.

于 2013-09-05T13:47:14.637 回答