tags:

views:

565

answers:

9
+8  Q: 

What is IIF in C#?

Possible Duplicate:
iif equivalent in c#

I have several lines of code using IIF in VB.

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 CSharp? Is there a short-circuit evaluation operator in C#?

+11  A: 

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

intCurrency = or.Fields["Currency"].Value == "USD" ? 100 : 0;
josh3736
Although you'l probably need to change those () to []. (on an unrelated point)
harpo
@josh3736 unfortunately that's not the equivalent. The C# ternary operator is both short circuiting and strongly typed while the VB.Net `iif` operator is neither.
JaredPar
And you have to cast the operands to object.
Hans Passant
It is, however, equivalent enough for most practical use; that is, at least in my experience.
kbrimington
@Jared `iif` is not even an operator, for that matter.
Anton Gogolev
@Anton, true, it is just a runtime function. Unfortunately my comment's been voted on so I can't edit it to correct it :(
JaredPar
A: 

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

Tom Brothers
A: 
intCurrency = or.Fields["Currency"].Value == "USD" ? 100 : 0;
Thomas
+1  A: 

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

intCurrency = or.Fields["Currency"].Value = "USD" ? 100 : 0;
Joe Doyle
+3  A: 

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
Thanks. That's the word I was looking for.
abhi
It isn't, it is called the conditional operator. Which belongs to the group of ternary operators. Small group.
Hans Passant
@Hans Passant: It *is* called **a** ternary statement, though one might call it **the** conditional operator.
Dario
It is an operator, not a statement, and should **not** be called a statement
MarkJ
@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
wow, didn't think a misstatement (pun intended) like that would cause such a heated discussion... Fixed.
joshperry
+1  A: 

It's the ?: operator.

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

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

nportelli
+1  A: 

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
Downvoter care to comment?
Adam Robinson
@Adam: Possibly due to the () instead of []? I upvoted myself, but that's the only thing I can think of...
Jon Skeet
@Jon: Thanks, good catch. That's what I get for copying from the question!
Adam Robinson
+20  A: 

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
+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
@Joe, Thanks for the correction, updated
JaredPar
+4  A: 

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
@Brian, to be super pedantic, the exact equivalent would use `object` instead of `int`. The `IIF` operator is a holdover from VB7 which did not have generic support hence the arguments and return were typed as `object`
JaredPar
@JaredPar: Haha...totally. I'll fix that :)
Brian Gideon
@bcat: Thanks for being pedantic and fixing the spelling of "padantic" :)
Brian Gideon
@Brian: Haha, any time. :)
bcat