-1

To find true or false with a boolean nullable variable, I can use

bool? nullable;
bool non-nullable;
non-nullable = (nullable == true);

or

...
non-nullable = (nullable ?? false);

It appears that the result is the same either way:

    nullable    non-nullable result
    --------    -------------------
    true        true
    false       false
    null        false

There certainly is a difference if these are integers, but I don't see any difference for this boolean example.
Is there any performance, or functional, difference between these?
For this boolean example, is there a reason to use one instead of the other?

Edit: fixed code - (nullable ?? true) should be (nullable ?? false)

Bob Krauth
  • 99
  • 2
  • 8
  • They are different. If `nullable` is `null`, `non-nullable` will be false in the first case (with ==) and true in the second case (with ??). – oerkelens May 18 '19 at 11:51
  • You are right. I messed up the example - it's fixed now. the '?? true' should be '?? false'. – Bob Krauth May 18 '19 at 11:59
  • Possible duplicate of [Null-condition and null-coalescing operator \*vs.\* plain boolean notation](https://stackoverflow.com/questions/49853515/null-condition-and-null-coalescing-operator-vs-plain-boolean-notation) – MadKarel May 18 '19 at 12:15
  • Performance-wise it doesn't really matter. Even if one is "heavier" then the other, it is probably too minor of a difference to make any difference. – Avi Meltser May 18 '19 at 12:19
  • Thanks for all the input - it has clarified it for me. In looking through the Possible Duplicate (thanks @MadKarel), it looks like there is no significant difference, as @AviMeltser says; it's more a question of code readabilty. – Bob Krauth May 18 '19 at 12:46
  • This is what's known as a *micro-optimization*. If there **is** a difference, it's negligible and will provide precisely zero real-world benefit, as your bottleneck is somewhere else. – Daniel Mann May 18 '19 at 13:30

2 Answers2

1

== 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)

  1. true == true, condition satisfies and returns true.
  2. false == true, condition not satisfies and returns false, so non-nullable will be false.
  3. null == true, condition not satisfies and returns false, so non-nullable will be false.

(nullable ?? false) why (nullable ?? true)

  1. 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.

  2. same as first point

  3. null ?? false , now nullable variable contains null value, so it will assign false to left hand side operand

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • Functionally, they appear to do the same thing. Is there any reason to use one over the other? – Bob Krauth May 18 '19 at 11:36
  • @BobKrauth, no, functionally both are not same, kindly check my updated answer. `==` this operator checks equality between two operand and `??` checks, operand is null or not, if not null then assign value of right hand side of operand to left hand side otherwise assign default value to it which is after `??` – Prasad Telkikar May 18 '19 at 11:40
  • @BobKrauth, Does it answer your question or still you have some doubts. If you have just post an comment – Prasad Telkikar May 18 '19 at 11:45
  • thanks for your answer. I edited the question to hopefully provide more clarity (and fix an error). I'm wondering if there is a reason to use one or the other? – Bob Krauth May 18 '19 at 12:01
  • You could look at the generated IL and see what it really gets translated to. My guess would be `??` gets translated to an `if (nullable != null) {return nullable.Value;} else {return false};` and according to [MSDN](https://learn.microsoft.com/en-us/dotnet/api/system.nullable-1.equals?view=netframework-4.8) so does the other case. – MadKarel May 18 '19 at 12:11
1

There is yet another possible expression in your case:

non_nullable = nullable.HasValue && nullable.Value;

I don't exactly know if this will actually be slower than the other specified variants, since the operators on the nullable types are probably overloaded in the Nullable<T> structure as well and would involve method invocations as well. If you want to be sure about that, you will have to investigate and/or benchmark it.

As for the whole discussion about performance: I think it is better to first express your code as "naturally" as possible for future maintenance. Investigate performance improvements only when necessary. As Donald Knuth said: "Premature optimization is the root of all evil."

My advice about which expression to use would be to initially use the one that expresses your intent as clearly as possible. My personal choice would be: nullable == true.

Bart Hofland
  • 3,700
  • 1
  • 13
  • 22
  • Thanks. I have been using 'nullable == true' - it gives me a chance to remember what I was thinking next time I look at the code. – Bob Krauth May 18 '19 at 13:46