Is string a value type or a reference type?
I just can't find a "good" explanation for this...
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
- it then does unsafe mutates (allocating a new string and copying when needed). Have you checked? And if so: what .net version? –
Vani string
is null
, precisely because string
is a reference type. It is incorrect to say that the default value of a string
is ""
, because: it isn't. –
Vani m_firstChar
is used internally as a place holder to act as a char*
pointer to the start of the data, but it does not contain a char[]
(or a reference to a char[]
). The object itself is the data. It is one of only 2 object types (the other being arrays) that are allocated by the runtime with variable length. –
Vani string
. Re StringBuilder
: they actually rewrote that in 4.something; back in 2009 it was a naked string
underneath, being mutated. The current implementation is much more complex with some kind of linked chain of parts, effectively. –
Vani String is an immutable reference type.
See the blog post Immutable types: understand their benefits and use them on immutability.
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
The string type represents a sequence of zero or more Unicode characters. string is an alias for String in the .NET Framework.
Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references. This makes testing for string equality more intuitive. For example:
string a = "hello";
string b = "h";
// Append to contents of 'b'
b += "ello";
Console.WriteLine(a == b);
Console.WriteLine((object)a == (object)b);
This displays "True" and then "False" because the content of the strings are equivalent, but a and b do not refer to the same string instance.
Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this. For example, when you write this code, the compiler actually creates a new string object to hold the new sequence of characters, and that new object is assigned to b. The string "h" is then eligible for garbage collection.
string b = "h";
b += "ello";
In many languages, there are two general types of things: those where a storage location of a defined type will actually hold an object of that type, and those where a storage location of a defined type will hold a reference to an object of that type which is stored elsewhere. There are also a number of types of semantics things may offer:
In some programming languages like C++, it is possible for both direct-stored and indirect-reference types to implement any of the above four types of semantics. In .net and C#, direct-stored types with exposed fields always implement #3, class types with exposed fields always implement #4, other value types may implement any of the above, and other reference types may implement #1, #2, or #4, but not #3, with an only-slightly-leaky abstraction. Strings implement #1.
Maybe the article Strings in .NET and C# can help you. According to this article, string is a reference type.
© 2022 - 2024 — McMap. All rights reserved.
NullReferenceException
when evaluated in the following:string s0 = null; Console.WriteLine(s0); string result = String.Concat(s0, "aaa");
Perhaps it just does not fit neatly into the acknowledged categories. – SocorrosocotraArgumentNullException
, but both methods explicitly say that they do not choose to do that;Console.WriteLine
says: "If value is null, only the line terminator is written to the standard output stream.";String.Concat
says "An Empty string is used in place of any null argument.". And that's perfectly fine. The only time we would typically expectNullReferenceException
is things likes0.Length
, which: indeed, throws that – Vani