Why string is a reference type?

I don't understand it.

System.String hola = "tal";
System.String hola2;

hola2 = "talycual";
Debug.Print(hola2);
hola2 = hola;
hola = "___";
Debug.Print(hola2);



And the output is:

talycual
tal

If string is a reference type ... the output must be at the last line : ____, but 'hola2' has a copy of 'hola' .

I understand differences between struct and class, and because is struc a value type and because is class a reference type but ... i don't understand because is string a reference type

Regards.



Answer this question

Why string is a reference type?

  • Amde

    In your example

    Do 'C = A', then 'C' point to '#A_address' (example). Any change in 'C' affect to 'A' and viceverse.

    But when do 'A = B' , you point 'A' to 'B' , then 'A' point to '#B_address' , refer a different object than 'C', if moddifing 'B' or 'A' don't affect to 'C'.

    It's OK! :)

    In my example:

    Do 'hola2 = hola', and 'hola2' must point to '#hola_address' .
    But when i do 'hola="___"' , 'hola2' must refer to '#hola_address' and then its value must be "___" like in your example when do 'C = A' and after 'A.Name="CCC"' .... 'C.Name' value is "CCC", but not, and it's i don't understand :S

    Thanks to both.

    Regards.


  • RickN

    MyClass A = new MyClass("AAAA");
    MyClass B = new MyClass("BBBB");
    MyClass C = A; // A == C == "AAAA"
    A.Name = "CCCC"; // A== C == "CCCC"
    A = B; // Now, A == B == "BBBB" while C == "CCCC"

    If you change a property of A, it will also affect C, since they refer to the same object. However, if you assign a new value to A, it will not affect C, as they now refer to different objects.

    This is exactly the behavior you example showed in regard to string (except, due to the nature of Strings, we cannot show a step where we change a property of the string).

    Basically, a string is a reference type that was implemented in a way that makes it act very much like a value type.



  • Cuneyt Deger

    See my posts:

    String Basics Part I

    String Basics Part II

    Hope that helps,



  • Lou_Davis

    What follows won't compile, but should help you see the difference.

    See your original code as:

    System.String hola = new System.String ("tal");
    System.String hola2;

    hola2 = new System.String ("talycual");
    Debug.Print(hola2);
    hola2 = hola;
    hola = new System.String ("___");
    Debug.Print(hola2);

    As I mentioned, this won't compile, since there isn't such a constructor... but would you now expect hola to be the same as hola2

    As to why this was made so, I expect that strings are generally too large to be pushed on the stack (and they have a variable size...), so making them valuetypes would have been an awful mess.

    HTH
    --mc


  • Rattlerr

    Scott Allen wrote:

    See my posts:

    String Basics Part I

    String Basics Part II

    Hope that helps,



    Very usefull, thanks !

    Regards.


  • jessicalegazpi

    Yes, the change after assinging the original value was intentionated, i want view if both variables point to same memory addres.

    Then, when i change the value of a string, the variable point to another/new memory address

    Sorry for my ignorance but, I don't understand the Value/Reference difference between a 'int' type and a 'string' type.

    Regards.


  • Izzy545

    Scott Allen wrote:

    See my posts:

    String Basics Part I

    String Basics Part II

    Hope that helps,



  • Okugops

    Scott Allen wrote:

    See my posts:

    String Basics Part I

    String Basics Part II

    Hope that helps,



    Thanks ! very usefull :)

    Regards.


  • Dietz

    "string is reference type but really special one. When you do operation of both string values like in your example
    C = A, there is nothing in this operation simular to normal class behavior.
    ....."

    People with clear mind like yours should be admired. It was ery helpful to read this.



  • Joel Martinez

    >> C = A, there is nothing in this operation simular to normal class behavior. So C doesn't have a reference of A. In that operation, a new string class is created and the value from string A is copied to string C. String C and String A are pointing in completly different memory address. <<

    Actually, I don't believe that is true. After C=A, both C & A refer to the same phyisical string in memory. This is safe, because there is no possible way (outside of an unsafe block) to modify that string. Any action that would appear to modify it (ToUpper, for example), actually creates a new string.



  • stang4lyfe

    string is reference type but really special one. When you do operation of both string values like in your example
    C = A, there is nothing in this operation simular to normal class behavior. So C doesn't have a reference of A. In that operation, a new string class is created and the value from string A is copied to string C. String C and String A are pointing in completly different memory address. That is way, when you have many string operation, for example loop and in every iteration you have a string manipulation, because of new string class generation, that operation is performance leak. In that situation it is preffered to use StringBuilder. Why, because StringBuilder doesn't create new string for any string operation.

    Now for question, why string is not value type and it's reference type.
    When you have let's say int value, Int type has defined lenght and that is 4 byte. And for every value type you know the exact memory space that will take. But for string you don't know that. It will be stupid to have a string type in wich you should always define how long it is. And if you want to put in that string variable a value with bigger lenght that string is defined, that will be disallowed.
    Why is string a special class. It is special because you have a reference class, but the way of using it, looks like a value type. So, that's why you do operation like:
    string C = A, and not string C = new string(A);
    You don't have to instance string class because .NET framework do that for you when you first time set it's value. If you not set it's value string has value of null like any other reference type. Every language platform has a special treatment for string type. .NET is not exclusion from that too.



  • proecco

    Ok!! , i understand yet :)

    A lot of thanks to all for your time.

    Regards.


  • Quack&amp;#33;

    you are modifying the original variable AFTER assigning the original value to another variable so it won't "update" it automatically if this is what you are asking.

    hola2 will be "Tal" since you pointed it to hola, but then after this assignment, you changed hola to be "___" but hola2 still has a reference to the "old" value.



  • Why string is a reference type?