Dhanasekar Murugesan
Dhanasekar Murugesan

Reputation: 3239

C#: Order of execution of operands in an IF statement

I came across some statements while surfing the .net source:

If( null != name)
If( false == check())

what is the difference between (name != null) and (Check() == false) statements from the above statements?

Can any one get me clear of this? Please.

Upvotes: 0

Views: 348

Answers (4)

Habib
Habib

Reputation: 223267

I think this style comes with C/C++ background, where the operator = which can also be used inside if statement, which could be used for comparison as well as assignment.

The statement if( null != name) is same as if(name != null), so as the other statement

To safeguard from mistakenly assigning the value to the left hand side, this style of check is used in C/C++,

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500675

I'm sure this is a duplicate, but I can't find it right now.

Code with the constant first is usually written by developers with an (old) background in C, where you might write this:

if (NULL == x)

instead of this:

if (x == NULL)

just in case you made a typo like this:

if (x = NULL) // Valid, but doesn't do what you want.

In most cases in C#, you don't need to do this - because the condition in an if statement has to be convertible to bool.

It would make a difference when you're comparing to a Boolean constant:

if ((Check() = false) // Eek!

... which is one reason to just write:

if (!Check())

Basically, putting the variable first is generally more readable - and in C# the reason for putting the constant first is almost never applicable anyway. (Modern C and C++ compilers will usually give you a warning anyway, so it's no longer a great idea in those languages either, as far as I'm aware...)

In terms of the order of evaluation, there is a theoretical difference in that the left-hand operand will be evaluated before the right-hand operand... but as the constants null and false don't have any side-effects, there's no real difference.

Upvotes: 4

Marc Gravell
Marc Gravell

Reputation: 1062865

The odd reversal looks like it was written by a C/C++ programmer... basically, in C/C++, if you accidentally type:

if(name = null)

instead of

if(name == null)

then that compiles and changes the value to null when invoked. A C/C++ habit to avoid this mistake is to use a constant such as null / false always on the left. This is because you can't reassign a constant, so it generates a compiler error if you use = instead of ==. In C# it (if(a = null)) doesn't usually compile, so that is not a concern.

There is no difference re the position here - both sides are evaluated. Idiomatic C# for this would be simply:

if(name != null) ...
if(!check()) ...

Upvotes: 0

hkutluay
hkutluay

Reputation: 6944

Take a look Coding Horror Yoda Conditions

Using if(constant == variable) instead of if(variable == constant), like if(4 == foo). Because it's like saying "if blue is the sky" or "if tall is the man".

Upvotes: 1

Related Questions