Difference between If and Iif

Asked

Viewed 3,045 times

5

In VB.NET, what is the difference of the use of If and of IIf under these conditions?

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

IIf(Condicao, x(), y())

1 answer

6


Commando If

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

Function IIf()

The IIf() is a function. So the first big difference is that it generates a result. She decides what to run based on the boolean expression used as the first argument. It has neither blocks of code for the true result, nor for the false one. What exists is a value that will be used as return. The second argument will be returned by the function if the Boolean expression is true. The third argument will be returned if it is false. Obviously this value can be composed of any expression.

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

Differences

But there is a 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 (or desired, objective-dependent side effects). At least that’s what the documentation says. I couldn’t build an example like the question that was true. I got the example of documentation. Do not ask me why the short circuit occurs in one situation and in another situation does not occur.

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

In these two examples not much changes since the result of the IIf() is discarded. In fact the results of x() and y() also are. And note that these functions need to return something. It cannot be a no return subroutine. The IIf() accepted. What will change cannot be determined by what is in the question. It is important if what x() and y() execute generate side effect. If yes, then something different will occur since in the function both will be executed.

Obviously the function tends to be a little slower, not only because functions requires a overhead (compiler could optimize) but mainly because it has something executed that may not be useful, even if it does not generate side effect.

Operator If()

Note that the function is considered obsolete. Prefer to use the operator If() for the same result. It works essentially the same but doing Circuit short of these expressions, that is the argument that does not need the result nor is it executed. This is the most common that is desired. But if you have any reason to want the execution of the other expression to be executed anyway, then 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 expression If() be the type String. The function can even do the implicit conversion in some cases, but will only be of the expected type after the required conversion. This is used Option Strict Off.

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

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

The operator cannot be used loose, needs to be in a place waiting for an expression.

Console.WriteLine(If(Condicao, x(), y()))
  • 1

    Just to cover, the IIf can also be used for null checker, for example, IIf(SeIssoForNulo, UseIsso).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.