'Inline IF' Statement??

  • Thread starter Thread starter ChrisM
  • Start date Start date
C

ChrisM

Hi,

Someone once showed me a C# statement that worked like:

myInt = (if <some condition>then 3 otherwise 4)

The syntax is totally wrong, (that's what I'm trying to remember). But it
worked like that.

He called it a 'Unary If' statement (or somthing like that)

ie I could assign either 3 or 4 to my integer variable myInt depending on
the evalutaion of the condition.

To put it another way, it was the equivelent of

if (<some condition>)
myInt = 3
else
myInt = 4

but in a single statement.

Does anyone know what the hell I'm talking about?
 
ChrisM said:
Someone once showed me a C# statement that worked like:

myInt = (if <some condition>then 3 otherwise 4)

The syntax is totally wrong, (that's what I'm trying to remember). But it
worked like that.

He called it a 'Unary If' statement (or somthing like that)

ie I could assign either 3 or 4 to my integer variable myInt depending on
the evalutaion of the condition.

To put it another way, it was the equivelent of

if (<some condition>)
myInt = 3
else
myInt = 4

but in a single statement.

Does anyone know what the hell I'm talking about?

Yes - I believe you're talking about the conditional operator:

myInt = condition ? 3 : 4;
 
Back
Top