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?
Much of the implementation of
System.Stringis in native code (C/C++) and not in managed code (C#). If you take a look at the decompiled code you’ll see that most of the “interesting” or “core” methods are decorated with this attribute:Only some of the helper/convenience APIs are implemented in C#.
So where are the characters for the string stored? It’s top secret! Deep down inside the CLR’s core native code implementation.