Point Deep

Mundeep's Tech Blog

c# if else alternative syntax using ? : operator

Posted by mundeep on July 15, 2011

One of the c# operators i tend to use a lot is the ?: operator which is essentially an alternative syntax to an if else statement. Unfortunately i often forget the order of the statements so am adding this quick post as a note for myself to easily remember :)

Essentially:

condition ? first_expression : second_expression;

Is equivalent to:

if (condition) {
 first_expression;
}
else {
 second_expression;
}

References:

About these ads

2 Responses to “c# if else alternative syntax using ? : operator”

  1. Anonymous said

    example
    if (token == “A”)
    tokenNumber = 1;
    else if (token == “B”)
    tokenNumber = 4;
    else if (token == “C”)
    tokenNumber = 5;
    else if (token == “X”)
    tokenNumber = 10;
    else
    tokenNumber = 20;

    is equivalent to

    tokenNumber = (token == “A”) ? 1:
    (token == “B”) ? 4:
    (token == “C”) ? 5:
    (token == “X”) ? 10:
    20;

  2. sanjeev said

    nice information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: