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有如下问题所指的If
语句,我认为:
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# 的三元条件运算符,第二个是它的合并运算符(返回 result
除非它是 Nothing
,在这种情况下返回 "Alternative"代码>).
If
因而取代了 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 的条件 If
运算符短路,因此您现在可以安全地编写以下内容,这是使用 IIf
函数无法实现的:
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屋!