Conditional Expression Difference

Is there a difference between the two statements

if (null != obj) 

and

 if (obj != null) 

If both are treated the same, what would be preferable?

+7
source share
7 answers

The difference here is in code generation. These two codes will not generate exact code, but in practice this will not affect the results or performance of the two statements.

However, if you create your own types and redefine the inequality operator and do a poor job, then that matters.

Consider this:

 public class TestClass { ... public static bool operator !=(TestClass left, TestClass right) { return !left.Equals(right); } } 

In this case, if the first argument of the operator is zero, i.e. if (null != obj) , then it will fire with a NullReferenceException .

So, we summarize:

  • The generated code is different
  • Performance and end results must be the same.
    • Unless you violated the code in the type used

Now I think that you are asking that you saw code from C that usually had code like this:

 if (null == obj) 

Please note that I switched to equality checking here. The reason is that a common mistake in programs written with old C compilers (these days they tend to catch this problem) would be to switch it and forget one of the equal characters, i.e. this is:

 if (obj = null) 

This sets the variable to null instead of comparing. Then the best way to deal with this error would be to switch it, since you cannot assign anything to null , it is not a variable. i.e. this will not compile:

 if (null = obj) 
+7
source

The first condition is Yoda. Use it, you should not.

+20
source

No, but the second method is more common and more readable (and more logical, in my opinion)

+4
source

No no. This is exactly the same.

The null == obj style is sometimes just used to prevent the regular typo obj = null from accidentally assigning a null value to a variable, but with != There is no reason to.

In .NET, it will not actually compile for typos obj = null .
Thus, the compiler does not allow you to accidentally do this.

The iodine condition comes from other languages โ€‹โ€‹where this compiler function is missing.

+2
source

They are exactly the same.

Some people prefer to put zero as the first part of an expression to avoid errors like this.

 if (obj = null) // should be obj == null 

But, of course, this does not apply to the != Operator, so in your example this is just a style difference.

+1
source

Using the first form

 if (blah == obj) 

stems from the days when compilers did not catch if (obj = blah) , that is, an unintentional assignment, unless the compilation warning level was set to a maximum

0
source

The first type of expression came from C / C ++ , where it was possible to pass non-logical values โ€‹โ€‹to check conditions. For example. everything that was not 0 was true, and zero was false:

 if (5) { } // true if (0) { } // false 

Sometimes this created problems if you forgot to enter one '=' char:

 if (x = 5) { } // this was true always and changed x value if (x == 5) { } // this was true, if x was equal to 5 

So, Yoda syntax was used to get a compiler error if one '=' is missing:

 if (5 = x) { } // this was generating compiler error for absent-minded programmers if (5 == x) { } // this was true, if x was equal to 5 

C # allows only boolean value in conditions, So

 if (x = 5) { } // this won't compile if (x == 5) { } // this is true, if x was equal to 5 

What about boolean types?

 if (y = true) { } if (y == true) { } 

Well, this is useless code because you can just write if (y). Conclusion: Yoda syntax is missing with C / C ++, and you no longer need to use it.

0
source

All Articles