<p>I recently asked a question about <a href="https://stackoverflow.com/questions/28377/iif-vs-if">IIf vs. If</a> and found out that there is another function in VB called <strong>If</strong> which basically does the same thing as <strong>IIf</strong> but is a short-circuit.</p>

<p>Does this <strong>If</strong> function perform better than the <strong>IIf</strong> function?  Does the <strong>If</strong> statement trump the <strong>If</strong> and <strong>IIf</strong> functions?</p>

[{'answer_id': 28498, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 5, 'selected': True, 'text': "<p>Damn, I really thought you were talking about the operator all along. ;-) Anyway …</p>\n\n<blockquote>\n  <p>Does this If function perform better than the IIf function?</p>\n</blockquote>\n\n<p>Definitely. Remember, it's built into the language. Only one of the two conditional arguments has to be evaluated, potentially saving a costly operation.</p>\n\n<blockquote>\n  <p>Does the If statement trump the If and IIf functions?</p>\n</blockquote>\n\n<p>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 <code>If</code> operator 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:</p>\n\n<pre><code>Dim result = If(a &gt; 0, Math.Sqrt(a), -1.0)\n\n' versus\n\nDim result As Double ' Redundant default initialization!\nIf a &gt; 0 Then\n    result = Math.Sqrt(a)\nElse\n    result = -1\nEnd If\n</code></pre>\n"}, {'answer_id': 23395769, 'author': 'BateTech', 'author_id': 2054866, 'author_profile': 'https://Stackoverflow.com/users/2054866', 'pm_score': 2, 'selected': False, 'text': '<p>One very important distinct between <code>IIf()</code> and <code>If()</code> is that with <code>Option Infer On</code> the later will implicitly cast the results to the same data type in certain cases, as where <code>IIf</code> will return <code>Object</code>.  </p>\n\n<p>Example: </p>\n\n<pre><code>    Dim val As Integer = -1\n    Dim iifVal As Object, ifVal As Object\n    iifVal = IIf(val &gt;= 0, val, Nothing)\n    ifVal = If(val &gt;= 0, val, Nothing)\n</code></pre>\n\n<p>Output:<br />\n<code>iifVal</code>  has value of Nothing and type of Object<br />\n<code>ifVal</code> has value of 0 and type of Integer, b/c it is implicitly converting Nothing to an Integer.</p>\n'}]
['https://Stackoverflow.com/questions/28478', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/299/']