开发者

Conditional Statements difference

Is there any difference bet开发者_如何转开发ween below two statements

if (null != obj)

and

if (obj != null)

If both treated same which will be preferable?


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


The difference here is the code generated. The two will not generate the exact same code, but in practice this will have no bearing on the results or performance of the two statements.

However, if you create your own types, and override the inequality operator, and do a poor job, then it will matter.

Consider this:

public class TestClass
{
    ...

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

In this case, if the first argument to the operator is null, ie. if (null != obj), then it will crash with a NullReferenceException.

So to summarize:

  • The code generated is different
  • The performance and end results should be the same
    • Except when you have broken code in the type involved

Now, the reason I think you're asking is that you've seen code from C, which typically had code like this:

if (null == obj)

Note that I switched to equality check here. The reason is that a frequent bug in programs written with old C compilers (these days they tend to catch this problem) would be to switch it around and forget one of the equal characters, ie. this:

if (obj = null)

This assigns null to the variable instead of comparing it. The best way to combat this bug, back then, would be to switch it around, since you can't assign anything to null, it's not a variable. ie. this would fail to compile:

if (null = obj)


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


No, there is not. It's exactly the same.

The style null == obj is sometimes just used to prevent the common typo obj = null to not accidently assign null to a variable, but with != there's absolutely no reason to do so.

In .NET it won't actually compile for the typo obj = null.
So the compiler prevents you from accidently doing it.

The Yoda condition comes originally from other languages, where this compiler feature is missing.


They are exactly the same.

Some people prefer to put the null as the first part of the expression to avoid errors like this

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

But of course this doesn't apply to the != operator, so in your example it's just a difference of style.


First type of statement came from C/C++, where was possible to pass not boolean values to condition verification. E.g. anything not 0 was true, and zero was false:

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

Sometimes it created problems if you forgot to type 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 receive compiler error in case one '=' was missed:

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# allow 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 gone with C/C++ and you do not need to use it anymore.


The use of the first form

if (blah == obj) 

stems from the days when compilers would not catch if (obj = blah) i.e. unintentional assignment, unless compile warning level was set to maximum

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜