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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. code doubt

code doubt

Scheduled Pinned Locked Moved C#
6 Posts 5 Posters 1 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.
  • A Offline
    A Offline
    anishkannan
    wrote on last edited by
    #1

    string str = "hai"; Console.WriteLine(str==str); Console.WriteLine("Hello"+str==str); Output: True False Have doubt in second,... why did not print "Hello" and why print False instead of True

    L D CPalliniC R 4 Replies Last reply
    0
    • A anishkannan

      string str = "hai"; Console.WriteLine(str==str); Console.WriteLine("Hello"+str==str); Output: True False Have doubt in second,... why did not print "Hello" and why print False instead of True

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, you should check the documentation on operator precedence to find out whether "Hello"+str==str is the same as "Hello"+(str==str) or is the same as ("Hello"+str)==str :)

      Luc Pattyn


      Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


      Local announcement (Antwerp region): Lange Wapper? Neen!


      D 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, you should check the documentation on operator precedence to find out whether "Hello"+str==str is the same as "Hello"+(str==str) or is the same as ("Hello"+str)==str :)

        Luc Pattyn


        Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


        Local announcement (Antwerp region): Lange Wapper? Neen!


        D Offline
        D Offline
        dybs
        wrote on last edited by
        #3

        Argh, beat me to it! :)

        1 Reply Last reply
        0
        • A anishkannan

          string str = "hai"; Console.WriteLine(str==str); Console.WriteLine("Hello"+str==str); Output: True False Have doubt in second,... why did not print "Hello" and why print False instead of True

          D Offline
          D Offline
          dybs
          wrote on last edited by
          #4

          Operator precedence.[^] Dybs

          1 Reply Last reply
          0
          • A anishkannan

            string str = "hai"; Console.WriteLine(str==str); Console.WriteLine("Hello"+str==str); Output: True False Have doubt in second,... why did not print "Hello" and why print False instead of True

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            anishkannan wrote:

            why did not print "Hello" and why print False instead of True

            I guess there's a common reason for... :rolleyes:

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • A anishkannan

              string str = "hai"; Console.WriteLine(str==str); Console.WriteLine("Hello"+str==str); Output: True False Have doubt in second,... why did not print "Hello" and why print False instead of True

              R Offline
              R Offline
              rakesh_choudhury
              wrote on last edited by
              #6

              For Console.WriteLine(str==str); here compiler checks "hai" is "hai" or not !! Compiler : Yes So your Console.WriteLine(str==str); is "True". For Console.WriteLine("Hello"+str==str); now the compiler checks is "Hellohai"(the outcome of "Hello"+str) is "hai". "Hellohai" == "hai" Compiler : no So your Console.WriteLine("Hello"+str==str); is "False". The key portion here is C# operator Precedence. :-O

              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