Possible Duplicate:
iif equivalent in c#
I have several lines of code using IIf in VB and I am trying to convert this code to C#.
Here’s an example where I need help:
intCurrency = IIf(or.Fields("Currency").Value = "USD", 100, 0)
How do I change the above line of code to C#? Is there a short-circuit evaluation operator in C#?
It is close to the C# ternary / conditional operator as several people have suggested but it is not an exact replacement. The C# ternary operator does short circuit evaluation meaning that only the side effect of the evaluated clause occurs. In VB.Net the
Iiffunction does not implement short circuiting and will evaluate both side effects.Additionally the
Iiffunction in VB.Net is weakly typed. It accepts and returns values typed asObject. The C# ternary operator is strongly typed.The closest equivalent you can write is the following. Putting the values into the arguments forces the evaluation of their side effects.
Or slightly more usable