How much space does string.Empty take in the CLR

How much space does string.Empty in the CLR?
I assume this is just one byte for the NULL character.

+4
string memory-management clr
source share
3 answers

No, a string is a complete object with an object header (containing a reference to the type, synchronization block, etc.), the length and any characters that must be a single null character (two bytes) and the corresponding padding to round to 4 or 8 bytes generally.

Note that although strings in .NET have a length field, they still end in zero for interaction. The null character is not included in length.

Of course, string.Empty will only refer to one object no matter how many times you use it ... but the link will be 4 or 8 bytes, so if you have:

 string a = string.Empty; string b = string.Empty; string c = string.Empty; 

it will be three links (12 or 24 bytes), all related to the same object (the size of which is probably about 20 or 24 bytes).

+8
source share

I think every .NET object needs an 8-byte header on 32-bit systems and a 16-byte header on 64-bit systems. But since string.Empty is generic (if used as a given constant), then only one instance is needed (since it is immutable and thus split - any use of it in the application does not matter, since I assume that the .NET platform is already uses it internally) and therefore only a pointer is required (4 or 8 bytes depending on the 32- or 64-bit host).

But the size of string.Empty itself must be 12-byte or 20-byte on 32-bit and 64-bit hosts.

+1
source share

byte per character ... Null accepts the character i ...

-one
source share

All Articles