user1908746
user1908746

Reputation:

Short-hand for return on true

I'm looking for a short-hand for return on true. The example code explains ii better:

if( error == true )
{
    return;
}

I'm asking about a short-hand version of the code above, basically something like:

error?return;

... or...

error?.return;

... or...

error||return;

... or whatever. You guys get the idea and, no, none of those work.

Thank you all.

Upvotes: -2

Views: 872

Answers (4)

Asif Ghanchi
Asif Ghanchi

Reputation: 224

Boolean returnvalue = error==true? true : false;

or

 Boolean returnvalue = error ? true : false;

Upvotes: -4

Mohit S
Mohit S

Reputation: 14064

The ternary operator ?: is not designed for control flow, it's only designed for conditional assignment. If you need to control the flow of your program, use a control structure, such as if/else.

the ternary operator is to initialize a variable with the result of the expression. At compile-time, the C# compiler translates the ternary expression into branch statements

?. is a new Null-Conditional Operator introduced in C#6.0

So the best approach would be

if(error) return

Upvotes: 1

Monza
Monza

Reputation: 755

I think it doesn't get much simpler than a slight reduction on your own code.

Perhaps:

if( error) return;

Upvotes: 2

JanR
JanR

Reputation: 6130

if( error == true )
{
    return;
}

Assuming error is a boolean you can shorten it this way:

if (error) return;

Upvotes: 0

Related Questions