0

Example 1:

var ca = p.GetCustomAttribute<ConnectorAttribute>();
return ca?.Point.IsEqual(cp) ?? false;

Example 2:

var ca = p.GetCustomAttribute<ConnectorAttribute>();
return (null != ca) && ca.Point.IsEqual(cp);

Question:

  • Does the two example return the same result?
  • Which one performs better?
  • Are there any concerns about thread safety?

Edit: Nobody mention but the title had some error, I've corrected it.

Edit 2: According the comments, 'those code are the same'. For me it is still not as trivial as it seams to be.

The answer here tells that the first part of example 1. creates a Nullable<bool> type. Is it optimized because of the null-coalescing operator?

minus one
  • 642
  • 1
  • 7
  • 28
  • 3
    Check the Intermediate Language. – Mary Apr 16 '18 at 09:09
  • 1
    `(null != ca)` - I spot a C/C++ programmer. ;) Anyway, code is going to be the same. As Mary says, check it by looking at the IL code. – Matthew Watson Apr 16 '18 at 09:10
  • 1
    "Does the two example return the same result?" It's only a handful of test cases, why do you ask us to do them instead of testing yourself. "Which one performs better?" [Race your horses](https://ericlippert.com/2012/12/17/performance-rant/) – René Vogt Apr 16 '18 at 09:12
  • 1
    According to [SharpLab](https://sharplab.io/#v2:CYLg1APgAgDABFAjAbgLACgoGYECY4DCcA3hnOQjlACxwCyAFAJQlkXtvvmdc8C+PHtjgAjAPZiANnAAqAUwDOAFwYAhOAGMADkx6l0XAG4BDAE6bjcALxwAdnIDucAILM0BruSgB2CwH4AOgAFMQBLWyUAgEkFAFEARwBXY0kGbRY/PzgAMxSFOXd2AXRijGEofGcMfQBIYXUQ8KUSAHM5JWRi0swqfFVqjDqccSk4GITk1PUxJQALOVNdD3J9T3I50zEneycAORmogFstSTlDuQi5YFiADw05LSVQsVs3fgxuoA===) these codes are the same. – Zohar Peled Apr 16 '18 at 09:17
  • 1
    `?.` operator usually copies operand to temp variable for thread-safety (similar to `var tmp = ca; if (tmp != null) ...`), but here `ca` is local variable and probably there is no reason to do this. Checking IL code might help indeed if you are curious. But semantically both code blocks are the same. – Evk Apr 16 '18 at 09:44

2 Answers2

2

Answer to your first question: Yes.

Short answer to your second question: None, you should choose based on which is more readable.

Answer to your third question: if you expect the whole expression to run "in one shot" and therefore not be subject to concurrency issues, then no, the null-coalescing operator does not guarantee that as explained in the answer of this Stackoverflow question. In both your examples you would actually face the same concurrency challenges.

Long answer to your second question:

Looking in the Microsoft '??' doc, all is mentioned is the operator purpose and function:

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.

Hence, the null-coalescing operator makes you write cleaner code that would otherwise require you to write the operand in question twice (as in your 2nd example).

Usage of the null-coalescing operator is more related to utility than performance, as explained is the accepted answer of a similar Stackoverflow question. Indeed, both perform quite the same.

Interesting to notice, as part of the same answer, the null-coalescing operator seems to perform slightly faster, but the difference is so little that could be ignored.

Community
  • 1
  • 1
smn.tino
  • 2,272
  • 4
  • 32
  • 41
0

To answer you question (null != ca) is equal to ca? as ?(this gives more readability and shorter version) it operator just check the value is null or not , Not sure but compiler underthe hood doing same thing , you can check using reflector.

Just to not both will give you same result and performs also be same , it just compiler of C# which under the hood replace ? with null check.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263