3

Possible Duplicate:
What is the difference between these (bCondition == NULL) and (NULL==bCondition)?

I have seen comparisons done both ways. Is there a performance difference or is it just personal preference?

I saw it being used in this answer:

What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)

Community
  • 1
  • 1
Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • 1
    OK - this is really just being picky. But I wonder if all the responders here are just making up the answer, or if they've actually compiled the code both ways and compared the resulting IL code to be sure it's identical. :) – James Johnston Aug 10 '11 at 15:21
  • 3
    There was an article that showed a slight improvement of using null == x as opposed to x == null. Article is here: http://geekswithblogs.net/manishsati/archive/2010/08/31/difference-between-comparisons-null-x-and-xnull.aspx – Xaisoft Aug 10 '11 at 15:21

5 Answers5

25

In this case, its personal preference in C#.

enter image description here

Yoda Conditionals

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
14

It's a rudiment from C++ days, where you could accidentally assign variable by using = instead of == and it would still pass the compiler cause you could pass almost anything into comparisons in C++. Do not use it in C#, cause it will not allow you to do so.

Valid C++:

if (p = NULL) // p gets assigned NULL and result is compared to 0

Invalid C#:

if (p = null) // can only use booleans in test

Grozz
  • 8,317
  • 4
  • 38
  • 53
6

Using null == x prevents accidentally mistyping and missing out one equals symbol and therefore assigning rather than testing for a value. You would end up with null = x which would not compile.

In any case, using Object.ReferenceEquals(x, null) is probably better anyway, as it would prevent cases where == has been overloaded.

iandotkelly
  • 9,024
  • 8
  • 48
  • 67
  • Interesting - ReferenceEquals works with Nullable types. I don't see the value in using it in this case, though. I have yet to encounter a situation where == null did not behave as expected. – TrueWill Aug 10 '11 at 15:31
  • @TrueWill. I've only seen one issue, and since then I've used ReferenceEquals(). It can't be common I agree, and in my opinion it is less readable which is a downside. But I did find a SO question with someone having a problem using == http://stackoverflow.com/questions/155458/c-object-is-not-null-but-myobject-null-still-return-false – iandotkelly Aug 10 '11 at 15:38
  • Given operator overloading and stupidity, it's possible to break many language idioms. I favor readability, simplicity, and doing my part to stamp out code that breaks this. – TrueWill Aug 10 '11 at 15:53
1

Absolutely identical in all aspects. Just a reversed way of doing the same thing.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
1

Is there a performance difference or is it just personal preference?

No, absolutely no performance difference. It's only a matter of personal preference.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928