Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance difference between IIf() and If

In Visual Basic, is there a performance difference when using the IIf function instead of the If statement?

like image 577
Bryan Roth Avatar asked Aug 26 '08 15:08

Bryan Roth


People also ask

What is the difference between IF and IIF in tableau?

Both the IF and IIF first check if the test is true; but the IIF then tests if the value is False. IF doesn't test for the False component – it treats everything not True in the same way. IIF handles those items that aren't True or False (or Unknown) differently to IF.

What is the use of IIF in VB net?

IIf always evaluates both truepart and falsepart, even though it returns only one of them. Because of this, you should watch for undesirable side effects. For example, if evaluating falsepart results in a division by zero error, an error occurs even if expr is True.


1 Answers

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") 

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.

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) 
like image 188
Konrad Rudolph Avatar answered Sep 19 '22 15:09

Konrad Rudolph