I recently asked a question about IIf vs. If and found out that there is another function in VB called If which basically does the same thing as IIf but is a short-circuit.
Does this If function perform better than the IIf function? Does the If statement trump the If and IIf functions?
Damn, I really thought you were talking about the operator all along. 😉 Anyway …
Definitely. Remember, it’s built into the language. Only one of the two conditional arguments has to be evaluated, potentially saving a costly operation.
I think you can’t compare the two because they do different things. If your code semantically performs an assignment you should emphasize this, instead of the decision-making. Use the
Ifoperator here instead of the statement. This is especially true if you can use it in the initialization of a variable because otherwise the variable will be default initialized, resulting in slower code: