IIf()和If之间的性能差异 [英] Performance difference between IIf() and If
问题描述
在Visual Basic中,使用 IIf
函数而不是 If
语句时是否存在性能差异?
In Visual Basic, is there a performance difference when using the IIf
function instead of the If
statement?
推荐答案
VB有以下如果
这个问题涉及的语句,我想:
VB has the following If
statement which the question refers to, I think:
' Usage 1
Dim result = If(a > 5, "World", "Hello")
' Usage 2
Dim foo = If(result, "Alternative")
第一个基本上是C#的三元条件运算符,第二个是它的coalesce运算符(返回结果
除非它是 Nothing
,在这种情况下返回替代
)。 如果
已取代 IIf
而后者已过时。
The first is basically C#'s ternary conditional operator and the second is its coalesce operator (return result
unless it’s Nothing
, in which case return "Alternative"
). If
has thus replaced IIf
and the latter is obsolete.
就像在C#中一样,VB的条件如果
运算符短路,所以你现在可以安全地编写以下内容,这是使用 IIf
function:
Like in C#, VB's conditional If
operator short-circuits, so you can now safely write the following, which is not possible using the IIf
function:
Dim len = If(text Is Nothing, 0, text.Length)
这篇关于IIf()和If之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!