Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. comapring condition in if

comapring condition in if

Scheduled Pinned Locked Moved ASP.NET
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yogsworld
    wrote on last edited by
    #1

    Hi friends What is difference bet 1) if (null == variableName ) And 2) if (variableName == null) is both are same ?????????

    C 1 Reply Last reply
    0
    • Y yogsworld

      Hi friends What is difference bet 1) if (null == variableName ) And 2) if (variableName == null) is both are same ?????????

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      yogsworld wrote:

      is both are same ?????????

      Yes, the are both the same. The reason you might see some people using the first varient is that in some languages (e.g. C, C++), if you made a mistake and typed

      if (variableName = null)

      by accident the compiler would accept it. (Note the single equal of assignment). By putting the constant on the left hand side of the operator the compiler would reject the assignment and the developer would realise they missed the extra equals sign. In .NET, the langage specififcation states that the result of an if statement must be a boolean so the compiler would reject an assignment in an if statement. It will give the error message

      Cannot implicitly convert type 'object' to 'bool'.

      If someVariable was a boolean the error message you'd get is

      Cannot convert null to 'bool' because it is a value type

      However, there is one scenario where the compiler will accept an assignment in an if statement. That is, if the assignment evalulates to a boolean. e.g.

      bool someVariable;
      if (someVariable = false) {}

      The compiler will compile this, but issue a warning instead

      Assignment in conditional expression is always constant; did you mean to use == instead of = ?

      Does this help?


      "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

      Y 1 Reply Last reply
      0
      • C Colin Angus Mackay

        yogsworld wrote:

        is both are same ?????????

        Yes, the are both the same. The reason you might see some people using the first varient is that in some languages (e.g. C, C++), if you made a mistake and typed

        if (variableName = null)

        by accident the compiler would accept it. (Note the single equal of assignment). By putting the constant on the left hand side of the operator the compiler would reject the assignment and the developer would realise they missed the extra equals sign. In .NET, the langage specififcation states that the result of an if statement must be a boolean so the compiler would reject an assignment in an if statement. It will give the error message

        Cannot implicitly convert type 'object' to 'bool'.

        If someVariable was a boolean the error message you'd get is

        Cannot convert null to 'bool' because it is a value type

        However, there is one scenario where the compiler will accept an assignment in an if statement. That is, if the assignment evalulates to a boolean. e.g.

        bool someVariable;
        if (someVariable = false) {}

        The compiler will compile this, but issue a warning instead

        Assignment in conditional expression is always constant; did you mean to use == instead of = ?

        Does this help?


        "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

        Y Offline
        Y Offline
        yogsworld
        wrote on last edited by
        #3

        hi i didn't get your ans ... I just simply ask difference bet if ( someVarial == null) and if (null == somevariable) i.e position of nulll should be first or not I know both expression are evaluating same result.but if u see some code they comaore like if (null == someVarible) i.e position of null is first why ?????? Here someVarible is an refrence type not a value type ...... YOgesh M

        C 1 Reply Last reply
        0
        • Y yogsworld

          hi i didn't get your ans ... I just simply ask difference bet if ( someVarial == null) and if (null == somevariable) i.e position of nulll should be first or not I know both expression are evaluating same result.but if u see some code they comaore like if (null == someVarible) i.e position of null is first why ?????? Here someVarible is an refrence type not a value type ...... YOgesh M

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          yogsworld wrote:

          position of null is first why ??????

          I explained why. It is a historical thing. Developers who programmed in languages such as C or C++ put null first as a way of protecting themselves by having the compiler flag an error to them. The error being the accidental missing equals sign. = means assignment == means a comparison These two concepts produce radically different results, yet are very very easy to accidentally mix up. Someone typing fast may miss one of the equals signs. In C# the compiler demands that the result of the conditional expression in an if statement evaluates to a Boolean value. I demonstrated the various error messages that the C# compiler would generate that a C or C++ compiler would not likely generate. I should clarify: Putting the null first is not completely the same as putting it second IF there is an error in the code. If the code is written correctly with the double-equals (comparison) operator then the code is the same. Does this make sense?


          "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups