title: VB.NET - IIF(,,) - Both "sides" are evaluated. What situations should I watch out for?
body: <p>I recently learned of the IIF(A,B,C) function. I'm a long time VB/VB.NET Coder who recently spent a lot of time coming up to speed in SQL coding.</p>
  <p>One (obvious) common thing to do in SQL is something like the following:</p>
  <pre><code>select (case where @var = 0 then MyTable.Val1 else MyTable.Val2 end) from MyTable
  </code></pre>
  <p>IIF(A,B,C) will allow me to do this in VB.NET... all on one line.</p>
  <p>However, I have read that both B and C are evaluated no matter what A evaluates to.</p>
  <p>I can think of some obvious situations where this is a bad thing such as:</p>
  <pre><code>Dim X as integer = IIF(SomeBoolean = true, ExpensiveFunction1(), ExpensiveFunction2())
  </code></pre>
  <p>As I will be including this in my repertoire, are there any more subtle situations where I may get myself into trouble using IIF?</p>
  <p>It's a pretty big departure in some situations from using the old fashioned:</p>
  <pre><code>Dim X as integer
  if SomeBoolean = true then
    X = ExpensiveFunction1()
  else
    X = ExpensiveFunction2()
  end if
  </code></pre>
  <p>I'm hoping to save myself some annoying performance issues and/or bugs in the future.</p>
  <h1>Update 2016</h1>
  <p>For the past few years, a new VB.NET feature exists which eliminates the need to use the IIF() function.</p>
  <pre><code>if(Something = true, ExecuteA(), ExecuteB())
  </code></pre>
  <p>Only ExecuteA() OR ExecuteB() are executed. Finally, inline IF with shortcircuiting.</p>
  <p>So, if you are using later versions of VB.NET (as of 2016), use this instead if you can.</p>