shorthand If Statements: C#

Just a quick one, Is there anyway to shorthand this?

It’s basically determining the direction left or right, 1 for left, 0 for right

In C#:

if (column == 0) { direction = 0; }
else if (column == _gridSize - 1) { direction = 1; }
else { direction = rand.Next(2); }

The statement following this will be:

if (direction == 1)
{
    // do something
}
else
{
    // do something else
}

If there isn’t, it doesn’t really matter! just curious:)

Shorthand for conditional statements

I am looking for a way to write something like this: if (product.Category.PCATID != 10 && product.Category.PCATID != 11 && product.Category.PCATID != 16) { } In a shorthand way like b

C# Comparison shorthand

I have this code: if (y == a && y == b && y == c && y == d …) { … } Is there some form of shorthand so that I can rewrite it as something like this? if(y == (a &&amp

Multiple statements if condition is true in shorthand if

I recently discovered the shorthand if statement and after searching online I couldn’t find a definite answer. Is it possible to execute 2 statements if the condition is true/false? int x = (expressi

Should I replace all my if/else statements with ternary shorthand if/else? (PHP)

I was reading about ternary shorthand if/else and am wondering if it would make sense or be more beneficial to replace all (or most) of my traditional if/else statements with the ternary shorthand? Wo

c# If else shorthand

In c# can I do something like this in a shorthand? bool validName = true; if (validName) { name = Daniel; surname = Smith; } else { MessageBox.Show(Invalid name); } I was just wondering if some

PHP Shorthand Syntax to AND statements

I am trying to implement the logical connective AND, and was wondering if this shorthand notation is allowed: $hasPermissions &= user_hasAppPermission($user_id, $permission); Or do i have to do t

Help with deciphering shorthand C

I’m trying to figure out some things with some firmware that was written for us. I’m not all that familiar with C and I think there’s some shorthand going on here that I’m just not getting. I don’t un

Why do the following expanded if shorthand statements not work in javascript?

This is my first attempt to write shorthand if statements however am befuddled by why the expanded versions don’t work quite the way I imagined they would. Code 1 – Does not work if(document.getElemen

Shorthand byte notation in C/C++?

It’s been awhile since I programmed in C/C++. For the life of me, I cannot remember (or find in Google) how to make this work. I thought there was a shorthand way of writing a repeating string of byte

C# Shorthand Property Question

So here is a bit of syntax that I have never seen before, can someone tell me what this means? Not sure if this is supposed to be some shorthand for an abstract property declaration or something or wh

Answers

Use the ternary operator

direction == 1 ? dosomething () : dosomethingelse ();

Yes. Use the ternary operator.

condition ? first_expression : second_expression;

To use shorthand to get the direction:

int direction = column == 0
                ? 0
                : (column == _gridSize - 1 ? 1 : rand.Next(2));

To simplify the code entirely:

if (column == gridSize - 1 || rand.Next(2) == 1)
{
}
else
{
}

Recently, I really enjoy shorthand if else statements as a swtich case replacement. In my opinion, this is better in read and take less place. Just take a look:

var redirectUrl =
      status == LoginStatusEnum.Success ? "/SecretPage"
    : status == LoginStatusEnum.Failure ? "/LoginFailed"
    : status == LoginStatusEnum.Sms ? "/2-StepSms"
    : status == LoginStatusEnum.EmailNotConfirmed ? "/EmailNotConfirmed"
    : "/404-Error";

instead of

string redirectUrl;
switch (status)
{
    case LoginStatusEnum.Success:
        redirectUrl = "/SecretPage";
        break;
    case LoginStatusEnum.Failure:
        redirectUrl = "/LoginFailed";
        break;
    case LoginStatusEnum.Sms:
        redirectUrl = "/2-StepSms";
        break;
    case LoginStatusEnum.EmailNotConfirmed:
        redirectUrl = "/EmailNotConfirmed";
        break;
    default:
        redirectUrl = "/404-Error";
        break;
}