Great, you know the reference type!
Now System.String
is what? This is class, right. So... Read you own text. All correct.
This question is interesting. Thank you.
But the string is very special class. It's the reference type, but assignment operator is defined so it logically behaves pretty much like a value type. In fact, this is
imitation of value semantic. Same thing with the operator "==". It is not your default comparison of instances of classes. Strings compare "semantically" not referentially. To see the difference, compare strings using
object.ReferenceEquals
. Objects can be different, referential identity different, but comparison returns
true
.
There are many good reasons to imitate value semantics in string, as well as making them classes. This has something to do with memory economy and non-mutable nature of strings while keeping intuitive semantic.
The essence of string design can be understood if you learn string
interning
:
http://msdn.microsoft.com/en-us/library/system.string.intern.aspx[
^].
—SA