在 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 之间的性能差异
发布评论