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. General Programming
  3. C#
  4. A simple question related to strings in c#

A simple question related to strings in c#

Scheduled Pinned Locked Moved C#
questioncsharp
6 Posts 4 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.
  • D Offline
    D Offline
    Diana Fernandez
    wrote on last edited by
    #1

    Why the outputs are not the same? Char[] aChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; Char[] bChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; string a = new string(aChar); string b = new string(bChar); object c = a; object d = b; Console.WriteLine(c == d); String e = "hello"; String f = "hello"; object g = e; object h = f; Console.WriteLine(g == h); Regards, Diana.

    M A G 3 Replies Last reply
    0
    • D Diana Fernandez

      Why the outputs are not the same? Char[] aChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; Char[] bChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; string a = new string(aChar); string b = new string(bChar); object c = a; object d = b; Console.WriteLine(c == d); String e = "hello"; String f = "hello"; object g = e; object h = f; Console.WriteLine(g == h); Regards, Diana.

      M Offline
      M Offline
      manoj_dhamija
      wrote on last edited by
      #2

      because achar and bchar are two character arrays while e and f are two string variables thus they r not equal reply by dhamija.manoj@gmail.com manojdhamija@yahoo.co.in manoj

      1 Reply Last reply
      0
      • D Diana Fernandez

        Why the outputs are not the same? Char[] aChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; Char[] bChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; string a = new string(aChar); string b = new string(bChar); object c = a; object d = b; Console.WriteLine(c == d); String e = "hello"; String f = "hello"; object g = e; object h = f; Console.WriteLine(g == h); Regards, Diana.

        A Offline
        A Offline
        AB7771
        wrote on last edited by
        #3

        try this one just change the following lines string a = aChar.ToString(); string b = bChar.ToString(); now run the program and see the outputs Regards, Pramod

        1 Reply Last reply
        0
        • D Diana Fernandez

          Why the outputs are not the same? Char[] aChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; Char[] bChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; string a = new string(aChar); string b = new string(bChar); object c = a; object d = b; Console.WriteLine(c == d); String e = "hello"; String f = "hello"; object g = e; object h = f; Console.WriteLine(g == h); Regards, Diana.

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          The compiler regognises that the e and f strings are identical, and uses the same value for them. As e and f references the same string, the objects g and h will also reference the same string. When you compare two object references, they will only be considered equal if they reference the same object. --- b { font-weight: normal; }

          D 1 Reply Last reply
          0
          • G Guffa

            The compiler regognises that the e and f strings are identical, and uses the same value for them. As e and f references the same string, the objects g and h will also reference the same string. When you compare two object references, they will only be considered equal if they reference the same object. --- b { font-weight: normal; }

            D Offline
            D Offline
            Diana Fernandez
            wrote on last edited by
            #5

            Hi You mean all the identical strings will have same reference? Thanks

            G 1 Reply Last reply
            0
            • D Diana Fernandez

              Hi You mean all the identical strings will have same reference? Thanks

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Yes. All string literals that are identical will be reused. --- b { font-weight: normal; }

              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