最近看了一段代码,如下: 就是红色那段不明白他为什么这么写,这样子有什么区别
public bool OpenDB(string strConnect)
{
try
{
if (null == oraConnection) {
oraConnection = new OracleConnection(strConnect);
oraCommand = new OracleCommand();
if(oraConnection.State != ConnectionState.Open)
{
oraConnection.Open();
}
oraCommand.Connection = oraConnection; oraAdapter = new OracleDataAdapter();
oraAdapter.SelectCommand = oraCommand;
}
return true;
}
catch(Exception e)
{
if(oraCommand != null)
{
oraCommand.Dispose(); 
}
RF.Globe.WriteLog("OpenDB",e.Message);
RF.Globe.WriteLog("OpenDB",strConnect);
return false;
}
}

解决方案 »

  1.   


    二者没什么区别
    为了安全,好的编程习惯用null ==oraConnection这种
      

  2.   

    null == oraConnection
    把null放前面.......没有分配内存空间的话就创建这个连接
    如果反过来就先判断oraConnection为不为空,吧null放前面我感觉保证了第一下就创建了连接,而不是把内存分给别的 创建对象,个人认为
      

  3.   

    if (null == oraConnection)
    就不用担心写成
    if (null = oraConnection)但是
    if (oraConnection == null)
    就要担心写成
    if (oraConnection = null)
    有时候打错字跟少打一个字的虫,会让人找到想哭.....
      

  4.   

    if (null == oraConnection)与 if (oraConnection==null)有什么区别
    没有区别的,只不过提倡用if (null == oraConnection)
    因为以前的c++编译器支持if (oraConnection==null)和if (oraConnection=null),都不报错,但是if (null = oraConnection)就会报错,就是防止你把判断当成赋值了
      

  5.   

    没区别,主要是为了防止写成(oraConnection = null)
      

  6.   

    没有区别
    但推荐使用 null == oraConnection
    可以防止把==写成=
      

  7.   

    有时有区别。既然ls都说了无区别的情况,我只举个有区别的例子:
    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
                bool b = p == null; //false
                bool d = null == p; //true
            }        static public bool operator ==(string b, Program a)
            {
                return true;
            }
            static public bool operator !=(string b, Program a)
            {
                return true;
            }
            
            
            static public bool operator ==(Program a, string b)
            {
                return false;
            }    
            static public bool operator !=(Program a, string b)
            {
                return false;
            }
        }
    }