Could you please give an explanation about the difference of Reference Type and being Mutable concepts in C#? String is reference type but inmutable. Please, give more details about what is the conceptual difference between these two concepts and why they are independent?
Asked
Active
Viewed 1,030 times
-2
-
2Is this a homework question? – Dai Jan 15 '14 at 21:04
-
3This question appears to be off-topic because it seems to be a homework question showing no effort, and it is not actually about a specific coding problem. – Eric Lippert Jan 15 '14 at 21:10
-
Yet it could still be valuable for future searches on this topic. We like it or not but SO is becoming the standard place to "research" stuff. – Adrian Zanescu Jan 15 '14 at 21:16
-
3@AZ: This question is not even answerable; it's like asking "what's the difference between the address of my house and the color of my house?" How would you even begin to answer such a question? – Eric Lippert Jan 15 '14 at 21:18
-
@Eric - by providing a definition for address and another for color. Then showing why they are unrelated. But yes, you do have a point – Adrian Zanescu Jan 15 '14 at 21:27
-
@EricLippert Eric, you're right. I should try to research more about it. But I was trying to have a specific thread with the differences between these concepts. – Alberto Montellano Jan 15 '14 at 21:38
3 Answers
9
They are orthogonal -
- Mutable = properties can be changed;
- Reference type = value is really a reference to an instance.
There's no requirement that reference types be mutable or vice-versa. String
is an immutable reference type. Structs are not reference types and can be mutable, but best practice is that they be immutable.

D Stanley
- 149,601
- 11
- 178
- 240
2
Reference vs. Value types refer to copying semantics. A reference type when it's passed around does not get copied, it's reference ("address") does. Value types do get copied entirely. Mutability is orthogonal to this. You can have mutable value types and immutable reference ones.

Adrian Zanescu
- 7,907
- 6
- 35
- 53
-1
A string is a reference type because it could be huge and thus wouldn't fit on the stack like value types do. It is still immutable, because you don't change the value of it, you make a copy and change the value of the copy.

Jeff Pegg
- 1
- 1
-
Value types don't have to be on the stack. Most of the times they are not. Only local function variables are (and not even those in some cases) - that is only an implementation detail. Your argument is not valid – Adrian Zanescu Jan 15 '14 at 21:19
-
@AZ, your conclusion of Jeff's argument being invalid is invalid ;) The major reasons of strings being ref-types are indeed memory-related ([see here](http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type), for example) – Jan 15 '14 at 21:41
-
@elongzo. But not stack related and more about copying large amounts of data - which is what structs are all about (copying). This is an important difference. There is a huge misconception in programming circles about structs and every time they are talked about they get placed in the same sentence with the "stack". That is wrong. – Adrian Zanescu Jan 15 '14 at 22:01
-
@elongzo - also there is nothing preventing String of being a value type that holds a reference to a char array. And that would have nothing to do either with memory issues and could be a perfectly reasonable and optimum implementation – Adrian Zanescu Jan 15 '14 at 22:05
-
Wild speculation here - string is indeed only holding a reference to a char array. I presume they made it a ref type so that on construction the GC is involved and that special cases the allocation of the array as well (strings get their own special heap). Otherwise it would be difficult for the GC to know if a char array is part of a string or not when allocating (structs are not allocated by the GC - in a lot of cases they are but you get my point i hope) – Adrian Zanescu Jan 15 '14 at 22:19