问一下这两种写法有什么不同,哪种更好,为什么?
(1)
object o;
if (o != null)
{
   //statement
}
(2)
object o;
if (null != o)
{
    //statement
}也就是 o!=null 与null!=o有什么区别?
(或o==null与null==o)

解决方案 »

  1.   

    以前是提倡null==o这种编码方式,因为如果不小心写成null=o是会出错的,
    但现在感觉也差不多,因为VS能够检测出这种错误并给出警告,应该也可以设置一下使编译通不过
      

  2.   

    null != o 看起来怪怪的,o != null 看起来很顺眼
      

  3.   

    没有。在远古的C++时代
    if(o=null)
    {
    }
    是合法的,它把o赋值成null了,虽然它的本意是比较。
    为了避免这种笔误,有些人喜欢if(null==o)的写法,因为if(null=o)永远是不合法的。
      

  4.   

    没有任何区别吧!C++中
    以前是提倡null==o这种编码方式,因为如果不小心写成null=o是会出错的,编码风格问题。当然支持null==o。
      

  5.   

    select distinct name,type,
    (select count(1) from table where t.name=name and t.type=type) as scount
    from table t