Tangible Software Solutions

The Most Trusted Name in Source Code Conversion

C# Equivalent to VB IIf

Although the conditional ternary operator (?) of C# can be used in a similar way to VB's IIf function, it is not strictly equivalent.  VB's IIf function needs to evaluate all arguments since it is a method (method calls always evaluate all arguments), but the ternary operator is able to bypass some evaluations (using short-circuit logic in a similar way to the logical operators && and ||).

If your VB code used IIf in a way which depended on the side-effects produced by evaluating all arguments (e.g., an argument to IIf could be a method call which changes some state), then your equivalent C# code will either have to be rewritten to avoid reliance on side-effects, or you would have to resort to including the IIf function in your C# code (via a reference to the Microsoft.VisualBasic assembly).

If you need to convert between VB and C# and you are depending on the results being reliable and accurate, then you will want to have Instant C#, the best VB to C# converter, or Instant VB, the best C# to VB converter, at your fingertips.

Copyright © 1997 - 2017 Tangible Software Solutions Inc.