简体   繁体   中英

Performance difference between ?? and ==

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)

== 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 true?? false , it checks for value of nullable , it contains value ie true then it will assign that value to left hand side operand.

  2. same as first point

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM