Question mark inside C # syntax

Possible duplicate:
Benefits of using a conditional ?: (triple) operator

Hi, I am browsing this freesource library and I saw this weird - at least for me - syntax

*currFrame = ( ( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg ) ) ? (byte) 255 : (byte) 0; 

currFrame is a byte type

diff, differenceThreshold and ThresholdNeg are of type Int.

What does the question mark do? What does this strange destination mean?

Thank you in advance

+4
source share
7 answers

Operator (? :) returns one of two values ​​depending on the value of the boolean expression. The following is the syntax of a conditional statement.

 condition ? first_expression : second_expression; 

C # link: http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

In your case, currFrame will be set to 255 if ( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg ) ( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg ) - true , otherwise 0 will be assigned.

+8
source

it's the same as

 if(( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg ) ) currFrame = (byte) 255 else currFrame = (byte) 0 
+7
source

This is a conditional statement.

+4
source

?: Operator (link to C #) :

The conditional operator (? :) returns one of two values ​​depending on the value of the Boolean expression. The conditional operator has the form

 condition ? first_expression : second_expression; 
+3
source

'?:' is a conditional statement, you can read about it here: http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx

+1
source

This is a ternary operator (see MSDN ). It follows the following syntax:

 result = condition ? result_if_condition_true : result_if_condition_false 
0
source
 if ( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg ) ) *currFrame = (byte) 255; else *currFrame = (byte) 0; 
0
source

Source: https://habr.com/ru/post/1341656/


All Articles