为什么IIF要评估这两个方面? 。网

Why IIF is evaluating the two sides ? .NET

我正在尝试以简单模式使用 IIF:

 Dim MyList As List(Of Double) = New List(Of Double)
 Dim ret As Double

 ret = IIf(MyList.Count > 0, MyList.Max(), 0)

MyList 中没有元素,但抛出 System.InvalidOperationException,"Sequence has no elements"。 为什么 IIF 正在评估这两个方面?

谢谢!

因为that is the old VB6 function, use the If operator做短路评估:

ret = If(MyList.Count > 0, MyList.Max(), 0)