Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

question mark inside C# syntax [duplicate]

Tags:

c#

Possible Duplicate:
Benefits of using the conditional ?: (ternary) operator

hi, I'm viewing this freesource library and I saw this weird - at least for me - syntax

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

currFrame is of type byte

diff, differenceThreshold and differenceThresholdNeg are of type Int.

What does the question mark do ? , what is this weird assign sentence suppose to mean ?

Thanks in advance

like image 863
musaab Avatar asked Nov 28 '22 18:11

musaab


2 Answers

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

condition ? first_expression : second_expression;

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

In your case currFrame will be assigned a value of 255 if ( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg ) is true, otherwise value 0 will be assigned.

like image 98
Jakub Konecki Avatar answered Dec 10 '22 13:12

Jakub Konecki


this is the same as

if(( diff >= differenceThreshold ) || ( diff <= differenceThresholdNeg ) )
     currFrame = (byte) 255
else
    currFrame = (byte) 0
like image 34
JAiro Avatar answered Dec 10 '22 12:12

JAiro