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

- 41,598
- 9
- 101
- 157

- 10,479
- 15
- 47
- 56
9 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)

- 9,301
- 6
- 48
- 68

- 530,221
- 131
- 937
- 1,214
-
5You didn't answer the performance question and also don't mention the side-effects of `IIf`. – jor May 04 '15 at 12:25
-
2@jor The last paragraph of my answer is about the side-effects. The performance isn’t really relevant when one of the options is obsolete. For what it’s worth, the native operator `If` is more efficient than the `IIf` function by far. – Konrad Rudolph May 04 '15 at 12:32
-
Am I missing something? all you demonstrate is the `If` function... You don't demonstrate how the syntax or usage differs between `IIf` and `If` – Don Cheadle Aug 07 '15 at 19:29
-
2@mmcrae That's correct and on purpose: as I've said, IIf is obsolete, so there's little sense in discussing it. That said, my last paragraph does discuss the relevant difference. That's all you need to know, really. – Konrad Rudolph Aug 07 '15 at 21:02
-
3I appreciate you want to support good standards and ensure newbies don't pick up bad practices, but someone may have a perfectly legitimate issue that has caused them to want to understand the difference better... Such as maintaining legacy code, being stuck with some other tool that uses it, etc. And `That's all you need to know, really` doesn't sound like a conducive attitude for a website devoted to knowledge sharing ;) – Don Cheadle Aug 07 '15 at 21:29
-
1@mmcrae Oh, I didn't mean to be dismissive, I just really meant that the internals of IIf are boringly trivial. It simply contains a conventional If statement, which implies that the last piece of code in my answer wouldn't run. – Konrad Rudolph Aug 08 '15 at 07:24
-
BTW: Avoid this type of thing... Dim name = IIf(myObject Is Nothing, "(not specified)", myObject.Name), as you will get a null reference exception if myObject is null. Both branches are evaluted, even if the result of only one branch will be returned. I personally also try to avoid anything written in VB, but I don't always have the choice. – Phil Mar 03 '17 at 13:54
IIf()
runs both the true and false code. For simple things like numeric assignment, this isn't a big deal. But for code that requires any sort of processing, you're wasting cycles running the condition that doesn't match, and possibly causing side effects.
Code illustration:
Module Module1
Sub Main()
Dim test As Boolean = False
Dim result As String = IIf(test, Foo(), Bar())
End Sub
Public Function Foo() As String
Console.WriteLine("Foo!")
Return "Foo"
End Function
Public Function Bar() As String
Console.WriteLine("Bar!")
Return "Bar"
End Function
End Module
Outputs:
Foo!
Bar!

- 6,974
- 4
- 45
- 40

- 6,235
- 2
- 32
- 43
Also, another big issue with the IIf is that it will actually call any functions that are in the arguments [1], so if you have a situation like the following:
string results = IIf(Not oraData.IsDBNull(ndx), oraData.GetString(ndx), string.Empty)
It will actually throw an exception, which is not how most people think the function works the first time that they see it. This can also lead to some very hard to fix bugs in an application as well.
[1] IIf Function - http://msdn.microsoft.com/en-us/library/27ydhh0d(VS.71).aspx

- 14,236
- 12
- 79
- 119
-
This is why if came to replace the old IIF, I used to have issues with IIF but IF saved adding many lines of code. – NiL Apr 24 '14 at 14:07
Better use If instead of IIf to use the type inference mechanism correctly (Option Infer On)
In this example, Keywords is recognized as a string when I use If :
Dim Keywords = If(String.IsNullOrEmpty(SelectedKeywords), "N/A", SelectedKeywords)
Otherwise, it is recognized as an Object :
Dim Keywords = IIf(String.IsNullOrEmpty(SelectedKeywords), "N/A", SelectedKeywords)

- 17,605
- 9
- 77
- 106
According to this guy, IIf can take up to 6x as long as If/Then. YMMV.

- 17,666
- 6
- 54
- 86
-
1In my empyrical calculations (10000000 cycles), I evaluated IIf being about 12x slower! – Phantômaxx Dec 01 '14 at 09:49
I believe that the main difference between If and IIf is:
If(test [boolean], statement1, statement2) it means that according to the test value either satement1 or statement2 will executed (just one statement will execute)
Dim obj = IIF(test [boolean] , statement1, statement2) it means that the both statements will execute but according to test value one of them will return a value to (obj).
so if one of the statements will throw an exception it will throw it in (IIf) anyway but in (If) it will throw it just in case the condition will return its value.

- 61
- 1
- 1
On top of that, readability should probably be more highly preferred than performance in this case. Even if IIF was more efficient, it's just plain less readable to the target audience (I assume if you're working in Visual Basic, you want other programmers to be able to read your code easily, which is VB's biggest boon... and which is lost with concepts like IIF in my opinion).
Also, "IIF is a function, versus IF being part of the languages' syntax"... which implies to me that, indeed, If would be faster... if for nothing else than that the If statement can be boiled down directly to a small set of opcodes rather than having to go to another space in memory to perform the logic found in said function. It's a trite difference, perhaps, but worth noting.

- 1,488
- 1
- 15
- 23
...as to why it can take as long as 6x, quoth the wiki:
Because IIf is a library function, it will always require the overhead of a function call, whereas a conditional operator will more likely produce inline code.
Essentially IIf is the equivalent of a ternary operator in C++/C#, so it gives you some nice 1 line if/else type statements if you'd like it to. You can also give it a function to evaluate if you desire.

- 29,277
- 14
- 101
- 140
Those functions are different! Perhaps you only need to use IF statement. IIF will always be slower, because it will do both functions plus it will do standard IF statement.
If you are wondering why there is IIF function, maybe this will be explanation:
Sub main()
counter = 0
bln = True
s = iif(bln, f1, f2)
End Sub
Function f1 As String
counter = counter + 1
Return "YES"
End Function
Function f2 As String
counter = counter + 1
Return "NO"
End Function
So the counter will be 2 after this, but s will be "YES" only. I know this counter stuff is useless, but sometimes there are functions that you will need both to run, doesn't matter if IF is true or false, and just assign value from one of them to your variable.

- 999
- 13
- 25
-
2if you need both to run, you should explicitly call them both and then do a standard If. Using `IIf()` in this way is just going to confuse people reading you code. – Spivonious May 09 '16 at 15:26
-
I didn't mean for the comment to come across as a criticism; I was just pointing out that using it in this way is not a good practice. – Spivonious May 10 '16 at 19:37