Conditional Statements difference

后端 未结 7 1523
日久生厌
日久生厌 2020-12-20 16:24

Is there any difference between below two statements

if (null != obj)

and

if (obj != null)

If both treate

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-20 16:40

    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.

提交回复
热议问题