== equality operator in C# and ?? is null-coalescing operator.
From MSDN site
The == (equality) and != (inequality) operators check if their
operands are equal or not.
The ?? operator is called the null-coalescing operator. It returns the
left-hand operand if the operand is not null; otherwise it returns the
right hand operand.
non-nullable = (nullable == true);
Above statements checks condition if nullable variable contains true then it assigns true value to non-nullable, otherwise assign false.
bool? nullable;
In your case you are creating nullable
boolean type variable, it means it can store either bool
value or null
non-nullable = (nullable ?? true);
In above statement, set non-nullable
to the value of nullable
if nullable
is NOT null; otherwise, set it to true(which is provided as a constant or default value after ??
).
nullable non-nullable result (nullable ?? true) why?
-------- ------------------- ------------------------
true true
false false
null false
(nullable == true) why? (replacing nullable with its value)
true == true
, condition satisfies and returns true.
false == true
, condition not satisfies and returns false, so non-nullable
will be false.
null == true
, condition not satisfies and returns false, so non-nullable
will be false.
(nullable ?? false) why (nullable ?? true)
true?? false
, it checks for value of nullable
, it contains value i.e. true
then it will assign that value to left hand side operand.
same as first point
null ?? false
, now nullable
variable contains null
value, so it will assign false
to left hand side operand