Is string a value type or a reference type?

Is string a value type or a reference type?

I just can't find a "good" explanation for this...


Console.WriteLine(typeof(string).IsClass); // true

It's a reference type.

It can't be a value-type, as value-types need a known size for the stack etc. As a reference-type, the size of the reference is known in advance, even if the size of the string isn't.

It behaves like you expect a value-type to behave because it is immutable; i.e. it doesn't* change once created. But there are lots of other immutable reference-types. Delegate instances, for example.

*=except for inside StringBuilder, but you never see it while it is doing this...


String is an immutable reference type.

See the blog post Immutable types: understand their benefits and use them on immutability.


String is a reference type.


The fundamental "explanation" is based on "what" is actually stored in the memory location allocated when you "declare" the variable for the thing. If the actual value of the thing is stored in the memory location referred to by the variable name, then it is a value type.

   int x;  // memory allocated to hold Value of x, default value assigned of zero

If, otoh, the memory slot allocated when you "declare" the variable will hold only some other memory address where the actual value (or values) will be stored, then it is a reference type.

   MyClass x;  // Memory allocated to hold an address, 
               // default address of null (0) assigned.  
               // NO MEMORY ALLOCATED for x itself 

or, if declaration includes initialization,

   MyClass x = new MyClass();  
     // Now, Memory slot (call it Addr1) is allocated to hold address of x, 
     // more memory (call it Addr2) is allocated to hold a new MyClass object.
     // New MyClass object created, stored in memory Addr2 (on the Heap)
     // Address of new object (Addr2) is stored in Addr1

for a string, the string is created on the Heap, and it's address goes in the memory slot allocated for the variable, so it is a reference type.


String is an immutable reference type which has certain qualities that give it the occasional appearance of being a value type