locked
a == null vs null == a RRS feed

  • Question

  • Hi,

     

    What is the difference between these expressions when comparing an object to null.

    a == null vs null == a

     


    Please do not forgot to mark it as an answer if it is. Thanks Gags
    Thursday, November 18, 2010 11:46 AM

Answers

  • There is no difference.  The later one was used by c++ developers in order to avoid errors like if (a=null) but in C# both if (a=null) and if (null=a) will produce a compile time error.
    MCTS, CodeProject MVP 2008
    Thursday, November 18, 2010 11:48 AM
  • a == null vs null == a

    no difference
    Alan-SY
    • Marked as answer by Mike Dos Zhang Monday, November 22, 2010 5:34 AM
    Thursday, November 18, 2010 5:21 PM
  • It is *usually* the norm to evaluate the variable in question against the null rather then make the comparison against the value in question. But as the other answerers have stated, technically there is no difference.

    It is like the evaluation of a bool. The *usual* way is like so:

    if (myBool) {......}

    rather then:

    if (mybool == true) {....}

    Or:

    if (true == myBool) {...}


    John Grove - DSS, Senior Software Engineer
    • Marked as answer by Mike Dos Zhang Monday, November 22, 2010 5:34 AM
    Thursday, November 18, 2010 5:40 PM
  • Hi,

    In C++, if(a = null) is a semantic error but if(null = a) is a compile time error. So, some of C++ developers try to write if(null == a) instead of if(a == null) because if they forget to type double-equal (==) and type one instead, a compile time error alert them :)

    In C#, this doesn't have any role as Giorgi mentioned.

    Sincerely,

    Yasser


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    • Marked as answer by Mike Dos Zhang Monday, November 22, 2010 5:33 AM
    Thursday, November 18, 2010 5:41 PM
  • While it is usually true that there is no difference, a difference can exist if the == operator is overloaded.

    But I guess it would take a really weird person to do such a thing.

    • Marked as answer by Mike Dos Zhang Monday, November 22, 2010 5:33 AM
    Thursday, November 18, 2010 11:20 PM

All replies

  • There is no difference.  The later one was used by c++ developers in order to avoid errors like if (a=null) but in C# both if (a=null) and if (null=a) will produce a compile time error.
    MCTS, CodeProject MVP 2008
    Thursday, November 18, 2010 11:48 AM
  • a == null vs null == a

    no difference
    Alan-SY
    • Marked as answer by Mike Dos Zhang Monday, November 22, 2010 5:34 AM
    Thursday, November 18, 2010 5:21 PM
  • It is *usually* the norm to evaluate the variable in question against the null rather then make the comparison against the value in question. But as the other answerers have stated, technically there is no difference.

    It is like the evaluation of a bool. The *usual* way is like so:

    if (myBool) {......}

    rather then:

    if (mybool == true) {....}

    Or:

    if (true == myBool) {...}


    John Grove - DSS, Senior Software Engineer
    • Marked as answer by Mike Dos Zhang Monday, November 22, 2010 5:34 AM
    Thursday, November 18, 2010 5:40 PM
  • Hi,

    In C++, if(a = null) is a semantic error but if(null = a) is a compile time error. So, some of C++ developers try to write if(null == a) instead of if(a == null) because if they forget to type double-equal (==) and type one instead, a compile time error alert them :)

    In C#, this doesn't have any role as Giorgi mentioned.

    Sincerely,

    Yasser


    DO YOU STORE AND VERIFY PASSWORDS USING KEYS?! LEARN A BETTER WAY DURING A QUICK SIMPLE HOW TO:
    How To: Storing and verifying passwords
    • Marked as answer by Mike Dos Zhang Monday, November 22, 2010 5:33 AM
    Thursday, November 18, 2010 5:41 PM
  • While it is usually true that there is no difference, a difference can exist if the == operator is overloaded.

    But I guess it would take a really weird person to do such a thing.

    • Marked as answer by Mike Dos Zhang Monday, November 22, 2010 5:33 AM
    Thursday, November 18, 2010 11:20 PM