-1

I have some confusion about string a reference type or value type, I have two variables of type string, I assign value to first variable and then assigned same variable to second variable, If i change anything on first variable value doesn't get change in second variable, as second variable is also pointing to the same location where first one does?

        string string1 = "abc";

        string string2 = string1;

        string1 = "xyz";

here string1 is holding value xyz and string2 is abc

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
Nitin Aggarwal
  • 451
  • 1
  • 7
  • 18
  • 1
    It's a class so it's a reference type. It's also immutable. – Jeroen Vannevel Sep 02 '14 at 23:33
  • @JeroenVannevel but why both variable are not holding value xyz? – Nitin Aggarwal Sep 02 '14 at 23:34
  • And it doesn't even matter that it's immutable. Here you are just putting another reference into the variable named `string1`, you are not actually modifying anything. – Jon Sep 02 '14 at 23:34
  • Have a look at this SO thread. There's a good discussion there. http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type – Sam Sep 02 '14 at 23:50

1 Answers1

5

Here's what's going on:

string string1 = "abc";

string1 is a reference to the string "abc" which lives on the heap somewhere.

string string2 = string1;

string2 is another reference, that just happens to point to the same place in memory as string1. That reference was copied over to string2 when you used the assignment operator. You now have two variables that point to the same place in memory.

string1 = "xyz";

string1 now points to a newly created string that contains "xyz". string2 still points over to "abc", which is still reachable and won't be garbage collected.

In any case, yes; String is a reference type.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • but how come stringbuilder is different from string in this case? – Nitin Aggarwal Sep 02 '14 at 23:42
  • 1
    Because `StringBuilder` is built differently and has different behavior than `string` does. `StringBuilder` has a different purpose; it contains mutation methods, and allows you to build up a string without incurring the cost of constantly creating new strings. – Robert Harvey Sep 02 '14 at 23:44
  • 1
    `StringBuilder` is optimized for building strings through a bunch of concatenations. If you did this with `System.String`, it'll have to re-allocate memory over and over again since strings are immutable. – Mike Christensen Sep 02 '14 at 23:46
  • that's what i wanted to know how string is different from other reference type? – Nitin Aggarwal Sep 02 '14 at 23:47
  • ..the other point to make is that `string` is a type well understood by the CLR. `StringBuilder` however is just a class that is part of the BCL. – Simon Whitehead Sep 02 '14 at 23:47
  • Yea, `StringBuilder` was written in C#. You can probably dig up the Mono implementation if you were so inclined. – Mike Christensen Sep 02 '14 at 23:48
  • 2
    @MikeChristensen: .NET implementation is freely available as well. http://referencesource.microsoft.com/#mscorlib/system/text/stringbuilder.cs#46 – Jeroen Vannevel Sep 02 '14 at 23:52
  • @NitinAggarwal in "how come stringbuilder is different from string in this case" are you asking why `= "foo"` does not have `new` in front of it and creates new string without `new`... ? – Alexei Levenkov Sep 03 '14 at 00:23