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 regarding String

A simple question regarding String

Scheduled Pinned Locked Moved C#
question
5 Posts 3 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.
  • U Offline
    U Offline
    uusheikh
    wrote on last edited by
    #1

    Hi, i'm confused. If String is a reference type, why b does not change if a is modified, since b is just referring to a? E.g.

    String a = "Hello";
    String b = a;

    a = a.ToUpper();

    Console.WriteLine(a);
    Console.WriteLine(b);

    HELLO
    hello

    Thanks

    L 2 Replies Last reply
    0
    • U uusheikh

      Hi, i'm confused. If String is a reference type, why b does not change if a is modified, since b is just referring to a? E.g.

      String a = "Hello";
      String b = a;

      a = a.ToUpper();

      Console.WriteLine(a);
      Console.WriteLine(b);

      HELLO
      hello

      Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      uus831 wrote:

      If String is a reference type, why b does not change if a is modified, since b is just referring to a?

      You're not passing a reference to a string, but invoking a method on such a reference, and putting the result back in a. The documentation[^] says that this method returns a copy of the string;

      ' Returns a copy of this String converted to uppercase.

      The method ToUpper is executed, and it returns a reference to a new string. This pointer is then stored in your A-variable, not touching B.

      I are Troll :suss:

      B 1 Reply Last reply
      0
      • U uusheikh

        Hi, i'm confused. If String is a reference type, why b does not change if a is modified, since b is just referring to a? E.g.

        String a = "Hello";
        String b = a;

        a = a.ToUpper();

        Console.WriteLine(a);
        Console.WriteLine(b);

        HELLO
        hello

        Thanks

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Because it's not a.ToUpper() but a = a.ToUpper() - ToUpper doesn't change anything, it returns a new string. If you change the actual string (yes you can, unsafe code), b should probably change

        U 1 Reply Last reply
        0
        • L Lost User

          Because it's not a.ToUpper() but a = a.ToUpper() - ToUpper doesn't change anything, it returns a new string. If you change the actual string (yes you can, unsafe code), b should probably change

          U Offline
          U Offline
          uusheikh
          wrote on last edited by
          #4

          Ok, i understand. Thanks.

          1 Reply Last reply
          0
          • L Lost User

            uus831 wrote:

            If String is a reference type, why b does not change if a is modified, since b is just referring to a?

            You're not passing a reference to a string, but invoking a method on such a reference, and putting the result back in a. The documentation[^] says that this method returns a copy of the string;

            ' Returns a copy of this String converted to uppercase.

            The method ToUpper is executed, and it returns a reference to a new string. This pointer is then stored in your A-variable, not touching B.

            I are Troll :suss:

            B Offline
            B Offline
            Ben Fair
            wrote on last edited by
            #5

            Yes, Microsoft purposely made the String type act like a value type, even though it is really a reference type; it is immutable. Any operations on a String produce a copy, or a partial copy, of the String; the underlying string data cannot be directly changed. This is why you have to do a = a.ToUpper(); in the first place, rather than the ToUpper() method directly changing the String. This is also the reason that the StringBuilder class exists, because it allows string manipulation without the extra copying, and thus has better performance in situations where lots of string manipulation will be happening.

            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