IIf() 和 If 之间的性能差异

编程入门 行业动态 更新时间:2024-10-26 20:27:30
本文介绍了IIf() 和 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 之间的性能差异

本文发布于:2023-10-21 17:12:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1514835.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:差异   性能   IIf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!