IIF function equivalent in C#

C

Chad Myers

Hi all
Is there an equivalent function in C#? Thanks a lot in
advance.

C# "?:" operator (aka inline-if):

(condition)? true-statemet : false-statement;

ms-help://MS.NETFrameworkSDKv1.1/csref/html/vclrfConditionalOperator.htm

-c
 
M

Mattias Sjögren

Is there an equivalent function in C#?

You'd typically use the ?: operator.

result = condition ? true_part : false_part;

But note that this operator only evaluates one of the expressions,
either the true or false part, whereas IIf is a function and therefore
both expressions are evaluated.



Mattias
 
J

Jon Skeet [C# MVP]

Is there an equivalent function in C#? Thanks a lot in
advance.

Could you tell us what you want the function to do? I'm not familiar
with it (and as you haven't said what language it's in, it's not easy
to find out elsewhere...)
 
J

Joe Delekto

Greets,

While you can use the ternary ? : operator to simulate the IIf statement
in Visual Basic, you will want to note that it does have a subtle
difference. In Visual Basic, both expressions (regardless of the result of
the logical expression) are evaluated, whereas in C#, only the expression
determined by the logical operation is evaluated. So, if in VB you use:

result = IIf(expression, GetFunction(), SetFunction())

As opposed to:

result = expression ? GetFunction() : SetFunction();

And result == true, then in VB, both GetFunction() and SetFunction() are
evaluated (called) and the return value of GetFunction() is assigned to
result. However, in C#, only GetFunction() is called and its return value
assigned to result.

I should point out that it's typically not good practice to use IIf() in
that fashion, but I've seen it done before.

Regards,

Joe
 
A

Andreas Håkansson

To whom it might interesst,

First of all, I would like to appologize to the original poster ot this
thread
for invading on it with my own personal question, but I took it that there
was
nothing left to add to the original question.

Now to free my curiosity. I wondered how .net would treat the ? condition
internally opposed to a normal if with out an else (the else would not
matter
in this case anyway), so I wrote two methods

public bool Foo(int a)
{
return (a == 1)? true : false;
}

public bool Bar(int a)
{
if( a == 1 )
return true;
return false;
}

And grabbed the IL for them.

public bool Foo(int a);

.maxstack 2
.locals (bool V_0)
L_0000: ldarg.1
L_0001: ldc.i4.1
L_0002: beq.s L_0007
L_0004: ldc.i4.0
L_0005: br.s L_0008
L_0007: ldc.i4.1
L_0008: stloc.0
L_0009: br.s L_000b
L_000b: ldloc.0
L_000c: ret


public bool Bar(int a);

.maxstack 2
.locals (bool V_0)
L_0000: ldarg.1
L_0001: ldc.i4.1
L_0002: bne.un.s L_0008
L_0004: ldc.i4.1
L_0005: stloc.0
L_0006: br.s L_000c
L_0008: ldc.i4.0
L_0009: stloc.0
L_000a: br.s L_000c
L_000c: ldloc.0
L_000d: ret

I did this under the assumption that internaly, they would do the same, just
a
different syntax, but the Bar method was one instruction longer, but what
really
made me wonder was the

L_0002: bne.un.s L_0008

call in the Bar function which is documented as

"Transfers control to a target instruction (short form) when two
unsigned
integer values or unordered float values are not equal."

Whats the deal on this puppy? I'm dead on new to trying to read the IL of my
code so I am perfectly sure the is a resonable answer to this, but I wont be
the
one to provide it ;)

//Andreas
 
J

Jon Skeet [C# MVP]

<"Andreas Håkansson" <andy.h (at) telia.com>> wrote:

I did this under the assumption that internaly, they would do the same, just
a different syntax, but the Bar method was one instruction longer, but what
really made me wonder was the

L_0002: bne.un.s L_0008

call in the Bar function which is documented as

"Transfers control to a target instruction (short form) when two
unsigned
integer values or unordered float values are not equal."

Whats the deal on this puppy? I'm dead on new to trying to read the IL ofmy
code so I am perfectly sure the is a resonable answer to this, but I wontbe
the one to provide it ;)

Basically one of them is branching if the condition *isn't* met, and
one is branching if the condition *is* met, that's all.

The difference in length is just due to the shorter version sharing the
stloc.0 instruction. I expect they'd end up as very similar JITted
code, just with the branch order reversed again.
 
A

Andreas Håkansson

Jon,

The thing that puzzled me was that the docs said the function checked
unsigned ints or unordered floats for equality and not ints, even though the
parameter is int. Where as the Foo method used the beq.s call =)

Sorry if I'm daft ;)

//Andreas

"Jon Skeet [C# MVP]" <[email protected]> skrev i meddelandet
<"Andreas Håkansson" <andy.h (at) telia.com>> wrote:

I did this under the assumption that internaly, they would do the same, just
a different syntax, but the Bar method was one instruction longer, but what
really made me wonder was the

L_0002: bne.un.s L_0008

call in the Bar function which is documented as

"Transfers control to a target instruction (short form) when two
unsigned
integer values or unordered float values are not equal."

Whats the deal on this puppy? I'm dead on new to trying to read the IL of my
code so I am perfectly sure the is a resonable answer to this, but I wont be
the one to provide it ;)

Basically one of them is branching if the condition *isn't* met, and
one is branching if the condition *is* met, that's all.

The difference in length is just due to the shorter version sharing the
stloc.0 instruction. I expect they'd end up as very similar JITted
code, just with the branch order reversed again.
 
J

Jon Skeet [C# MVP]

The thing that puzzled me was that the docs said the function checked
unsigned ints or unordered floats for equality and not ints, even though the
parameter is int. Where as the Foo method used the beq.s call =)

Sorry if I'm daft ;)

Not at all. I think the point is that it doesn't really matter whether
you treat it as an int or an unsigned int, so long as you're only
looking for equality, and not actual ordering.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top