views:

1533

answers:

6

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

+8  A: 

String is a reference type.

Darin Dimitrov
+10  A: 

String is an immutable reference type.

link on immutability:

http://codebetter.com/blogs/patricksmacchia/archive/2008/01/13/immutable-types-understand-them-and-use-them.aspx

Kevin
+1 for immutable bit
Hemant
+1  A: 

Hi Schwertz

Maybe this article can help you. According to this article, string is a reference type.

Regards,

hadi

hadi teo
+21  A: 
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...

Marc Gravell
+1. I really dislike the lower-case string "type." Looks like a value type, behaves like a reference type.
ojrac
What has the case got to do with it? It is just an alias... object is reference-type, for example.
Marc Gravell
+2  A: 

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

JaredPar
+2  A: 

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.

Charles Bretana