To become better at .NET I found it helpful to look into the .Net-Framework itself and how Microsoft implements all the nice things. The last time I used my decompiler to have a look at the implementation of System.String-Class
, to research how a String-Object stores its actual string. I was surprised about the largest of the class in self and that I can't clearly identify which part of the class the actual string saves.
Here are two screenshot of the decompiling which shows the start and the end part of the class:
I suppose that [System.Reflection.DefaultMember("Chars")]
(first line in first screenshot) and/or the property public extern char this[int index]
(middle of second screenshot) the what I am looking for.
Is that the correct and if yes how does it work?
System.String
is a very special type. It's the only type in .NET other than arrays where different instances can have different sizes. (Anything else which "appears" to have different sizes such as List<T>
usually depends on an array, or some recursive type like a LinkedList
node. The objects themselves are of a fixed size.) The character data is inline within the object itself, along with the length of the string. It's not like a String
holds a reference to a char[]
or similar.
The CLR has very deep knowledge of System.String
, and a lot of it is implemented in native code. Basically, I would recommend against trying to understand the implementation at this point - it's likely to be more confusing than helpful.
The Chars
member (the indexer in C#) only fetches a single character from the string. You could follow that to find out more about where the data is stored, but it doesn't perform the actual storage itself.