tags:

views:

328

answers:

6

Dupe: http://stackoverflow.com/questions/302701/null-difference

A lifetime ago I came across an article that explained that the following were not equal (in c#):

if (o == null) {}
if (null == o) {}

The article explained that the latter was preferred because it resulted in a more accurate test. I've been coding like that ever since. Now that I understand so much more I was looking for the article, or another like it, to see what the exact findings were, but I can't find anything on the subject.

Thoughts? Is there a difference? First glance would say no. But who knows what happens in the bowels of IL and C# compilation.

+8  A: 

it's an old habit to prevent you from typing if (o = null). if (null = o) is a syntax error. kind of pointless in C#, because null values aren't ever coerced into booleans.

Jimmy
A: 

Irrelevant for C#, this is from C. See this question for a discussion.

orip
+2  A: 

In c# there's no difference. It's an old habit from C/C++ developers to avoid a common bug where this is correct syntax:

if(o = null)

In C# that wouldn't compile but in C and C++ it would, leaving you with a pretty nasty bug. Therefore, many people got used to doing

if(null == o)
BFree
+3  A: 

The latter is a holdover from the C/C++ days, where it was possible to accidentally assign a value instead of compare. C# won't allow you to do this, so either/or is acceptable (but I find the former more readable).

Bullines
A: 

Duplicate: http://stackoverflow.com/questions/302701/null-difference

Sunny
Then edit the question, and vote to close. This is not an answer.
Geoffrey Chetwood
@Rich: Yes, but I have no rights to close the question as dup. So instead of voting me down, do the right thing and close the question with a dup note. With your rep you should be able to do this.
Sunny
@Sunny, upvoted you anwyay but a comment on the question is probably better than an answer for this
Paul
@Sunny: This answer is not helpful to answer the question, so it is downvoted. Next time leave it as a comment or edit. I can only vote to close anyway. You can always just delete the answer.
Geoffrey Chetwood
This answer *is* helpful - one more click and you're at the answer. I really don't see this as being a problem - and it's more prominent than a comment would have been, too.
Jon Skeet
@Paul: Thanks. It's not about the votes and the points. Is about the correct behavior. And you I right, I had to realize that I can edit the question. Just my experience with other similar situations is that the right thing is to close it, and I couldn't, so I overlooked the edit option.
Sunny
A: 

I work with Java ..and I do have the habit of having constants on the LHS for all commutative comparisons.

"name".equals(name)

 null == obj

"55".compareTo(numString)

etc..Just avoiding unnecessary NullPointerExceptions...

Prasanna S
For string literals (i.e. the first and third), that makes sense. For the middle one, it's an unnecessary readability penalty.
Jon Skeet