Difference between If and IIf

4

In VB.NET, what's the difference between using If and IIf under these conditions?

If Condicao Then
    x()
Else
    y()
End If

IIf(Condicao, x(), y())
    
asked by anonymous 23.09.2016 / 04:18

1 answer

6

Command If

The If is a command ( statement ). It decides whether to execute a block of commands based on a Boolean expression. It can decide between a block or another if there is the Else part. The first block is executed if the result of the expression contained in If is true. And it will execute the second block if it gives false.

Function IIf()

IIf() is a function. So the first big difference is that it generates a result. It decides what to execute based on the Boolean expression used as the first argument. It has no code blocks for either the green result or the false. What exists is a value that will be used as a return. The second argument is returned by the function if the Boolean expression is true. The third argument will be returned if it turns out to be false. Obviously this value can be any expression.

The function can only be used with Imports Microsoft.VisualBasic .

Differences

But there is one fundamental difference. All three expressions of the function arguments are executed. One of the two results will not be used, but there will be execution. This can generate unwanted side effects (or desired, depends on the goal). At least that's what the documentation says. I could not build an example just like the question that was true. I got the example documentation ( see dotNetFiddle ). Do not ask me why a short circuit occurs in one situation and another does not.

In these two examples does not change much since the result of IIf() is discarded. In fact the results of x() and y() are also. And note that these functions need to return something. It can not be a non-return subroutine. IIf() accepts. What will change can not be determined by what is in the question. It is important that what x() and y() perform generate side effects. If yes, then something different will occur since in the function both will be executed.

Obviously the function tends to be a bit slower, not only because functions impose an overhead (the compiler could optimize) but mainly because it commands something that may not work, even if it does not generate collateral effect.

Operator If()

Note that the function is considered obsolete. Prefer to use the If() operator for the same result. It works essentially the same but doing short circuit of these expressions, that is the argument you do not need of the result is not executed. This is the most common thing you want. But if you have any reason you want the other expression to be executed anyway, you can use the function.

The operator has an extra advantage over the function. The function always returns a Object and may need a conversion. The operator returns the type used in the two result expressions. So if the second and third operand is a string , the result of every If() expression will be of type String . The up function may make the conversion implicit in some cases, but will only be of the expected type after the required conversion. This will use Option Strict Off .

The If() operator is closest to the If command at the point of execution. The operator is the same as the C # conditional operator ( ? : ) .

The operator can be used with two operands. Then it works like null-coalescing .

The operator can not be used loose, needs to be in a place that expects an expression.

Console.WriteLine(If(Condicao, x(), y()))
    
23.09.2016 / 04:59