17

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#?

Community
  • 1
  • 1
abhi
  • 3,082
  • 6
  • 47
  • 73
  • 22
    For reference, `Iif` is *not* a short-circuit evaluation. **The true and false expressions both get evaluated.** Make sure whenever you use it, that the expressions have no side effects that you care about. C#'s does only evaluate the corresponding expression, so that's something to watch for when you're converting. – cHao Aug 27 '10 at 18:13

9 Answers9

40

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 Iif function does not implement short circuiting and will evaluate both side effects.

Additionally the Iif function in VB.Net is weakly typed. It accepts and returns values typed as Object. 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.

public static object Iif(bool cond, object left, object right) {
  return cond ? left : right;
}

Or slightly more usable

public static T Iif<T>(bool cond, T left, T right) {
  return cond ? left : right;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 2
    +1, not only the closest equivalent, but I'd say exactly equivalent to the public Microsoft.VisualBasic.Interaction.IIf method, as can be seen using Reflector. But just to be pedantic, in VB.NET, IIf is a function not an operator. – Joe Aug 27 '10 at 19:30
  • @Joe, Thanks for the correction, updated – JaredPar Aug 27 '10 at 21:01
18

Yep, it's the question mark (A.K.A the conditional operator).

intCurrency = or.Fields["Currency"].Value == "USD" ? 100 : 0;
bdukes
  • 152,002
  • 23
  • 148
  • 175
josh3736
  • 139,160
  • 33
  • 216
  • 263
5

This is being incredibly pedantic, but the closest equivalent of IIf in C# is

intCurrency = ((Func<bool, object, object, object>)((x, y, z) => x ? y : z))(or.Fields["Currency"].Value == "USD", 100, 0);

However, I wonder if you are really interested in VB's ternary operator. So the following in VB

intCurrency = IIf(or.Fields("Currency").Value = "USD", 100, 0) 

could be better written as (notice the difference between IIf and If)

intCurrency = If(or.Fields("Currency").Value = "USD", 100, 0) 

which is exactly the same in C# as

intCurrency = or.Fields["Currency"].Value == "USD" ? 100 : 0;

Another interesting point is that If doubles as the null coalescing operator.

Dim text As String = Nothing
text = If(text, "Nothing")

which is exactly the same in C# as

string text = null;
text = text ?? "null";
Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
3

I know others have answered this... but I thought I'd clarify. If you're interested in more reading, it's called a ternary operator or also commonly known as the conditional operator.

joshperry
  • 41,167
  • 16
  • 88
  • 103
  • Thanks. That's the word I was looking for. – abhi Aug 27 '10 at 18:18
  • 3
    It isn't, it is called the conditional operator. Which belongs to the group of ternary operators. Small group. – Hans Passant Aug 27 '10 at 18:26
  • @Hans Passant: It *is* called **a** ternary statement, though one might call it **the** conditional operator. – Dario Aug 27 '10 at 18:36
  • It is an operator, not a statement, and should **not** be called a statement – MarkJ Aug 27 '10 at 18:54
  • @Dario it's a minor point but @Hans is correct here. The C# lang spec refers to this as the conditional operator (section 7.13) but notes it is also called the ternary operator. It makes no mention of a ternary statement. Ternary is only used in 2 sections of the spec. The aforementioned and 7.2 where it states the ternary class of operators which has 1 member the conditional – JaredPar Aug 27 '10 at 18:55
  • wow, didn't think a misstatement (pun intended) like that would cause such a heated discussion... Fixed. – joshperry Aug 27 '10 at 22:53
2

It's the ?: operator.

 or.Fields["Currency"].Value == "USD" ? 100 : 0;

http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

nportelli
  • 3,934
  • 7
  • 37
  • 52
1
intCurrency = or.Fields["Currency"].Value == "USD" ? 100 : 0;
Thomas
  • 63,911
  • 12
  • 95
  • 141
1

You can use the ?: operator to do equivalent code:

intCurrency = or.Fields["Currency"].Value = "USD" ? 100 : 0;
Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
1

As others have stated, you're looking for the conditional operator:

intCurrency = or.Fields["Currency"].Value == "USD" ? 100 : 0;

However, just as a point of correction, this is not "short circuit evaluation". Short-circuit evaluation means that once a boolean expression can be reliably determined, no further evaluation is performed (this amounts to encountering a true condition in an or expression or a false condition in an and expression).

While it's possible to combine boolean expressions in the conditional operator just like you can in anything else (like an if statement) and that expression will employ short-circult evaluation, it doesn't have anything to do, per se, with the conditional operator.

An important distinction to note is that, while VB.NET's Iif function evaluates both the true and false expressions, C#'s conditional operator evaluates only the expression that is selected by the boolean condition (making it behave more like a traditional if expression). This could be important if the VB.NET code relied on both expressions being evaluated.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
0

or.Fields("Currency").Value = "USD" ? 100 : 0

Tom Brothers
  • 5,929
  • 1
  • 20
  • 17