Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the internals of System.String

I used reflection to look at the internal fields of System.String and I found three fields:

m_arrayLength

m_stringLength

m_firstChar

I don't understand how this works.

m_arrayLength is the length of some array. Where is this array? It's apparently not a member field of the string class.

m_stringLength makes sense. It's the length of the string.

m_firstChar is the first character in the string.

So my question is where are the rest of the characters for the string? Where are the contents of the string stored if not in the string class?

like image 901
BowserKingKoopa Avatar asked Jan 21 '10 06:01

BowserKingKoopa


People also ask

What is a system string?

A string is a sequential collection of characters that's used to represent text. A String object is a sequential collection of System. Char objects that represent a string; a System. Char object corresponds to a UTF-16 code unit. The value of the String object is the content of the sequential collection of System.

How are strings stored internally in C#?

A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. There's no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters ('\0').

What is string in C# with example?

In C#, a string is a sequence of characters. For example, "hello" is a string containing a sequence of characters 'h' , 'e' , 'l' , 'l' , and 'o' . We use the string keyword to create a string.

What is string manipulation in C#?

In C#, string is an object of System. String class that represent sequence of characters. We can perform many operations on strings such as concatenation, comparision, getting substring, search, trim, replacement etc.


1 Answers

The first char provides access (via &m_firstChar) to an address in memory of the first character in the buffer. The length tells it how many characters are in the string, making .Length efficient (better than looking for a nul char). Note that strings can be oversized (especially if created with StringBuilder, and a few other scenarios), so sometimes the actual buffer is actually longer than the string. So it is important to track this. StringBuilder, for example, actually mutates a string within its buffer, so it needs to know how much it can add before having to create a larger buffer (see AppendInPlace, for example).

like image 184
Marc Gravell Avatar answered Sep 20 '22 04:09

Marc Gravell