Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

?: ?? Operators Instead Of IF|ELSE

public string Source {     get     {         /*         if ( Source == null ){             return string . Empty;         } else {             return Source;         }         */         return Source ?? string.Empty;     }     set     {         /*         if ( Source == null ) {             Source = string . Empty;         } else {             if ( Source == value ) {                 Source = Source;             } else {                 Source = value;             }         }         */         Source == value ? Source : value ?? string.Empty;         RaisePropertyChanged ( "Source" );     } } 

Can I use ?: ?? operators EXACTLY as If/Else?


My Question :
How to write the following with ?: ?? operators

[ 1 ]

if ( Source == null ){     // Return Nothing } else {     return Source; } 

[ 2 ]

if ( Source == value ){     // Do Nothing } else {     Source = value;     RaisePropertyChanged ( "Source" ); }  

Briefly : How to do nothing, return nothing and do multiple instructions with ?: ?? operator?

like image 707
Ahmed Ghoneim Avatar asked Jun 01 '11 21:06

Ahmed Ghoneim


People also ask

Which operator can be used instead of if-else?

The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way. First, you need to write a conditional expression that evaluates into either true or false .

Is there a ?: operator in Python?

The ternary operator is a way of writing conditional statements in Python. As the name ternary suggests, this Python operator consists of three operands. The ternary operator can be thought of as a simplified, one-line version of the if-else statement to test a condition.

What is ?: In Java?

The ternary conditional operator ?: allows us to define expressions in Java. It's a condensed form of the if-else statement that also returns a value.

Which is called ternary operator ?: && ===?

The conditional operator (? :) is a ternary operator (it takes three operands).


2 Answers

For [1], you can't: these operators are made to return a value, not perform operations.

The expression

a ? b : c 

evaluates to b if a is true and evaluates to c if a is false.

The expression

b ?? c 

evaluates to b if b is not null and evaluates to c if b is null.

If you write

return a ? b : c; 

or

return b ?? c; 

they will always return something.

For [2], you can write a function that returns the right value that performs your "multiple operations", but that's probably worse than just using if/else.

like image 127
trutheality Avatar answered Sep 20 '22 13:09

trutheality


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.

like image 37
Oliver Charlesworth Avatar answered Sep 23 '22 13:09

Oliver Charlesworth