How does String.Length work in .NET?
.net, c#
Solution
It doesn't. The number of characters is stored as a field (`m_stringLength`, if it matters); `.Length` simply returns this value. In .NET, strings are not typically nul-terminated or similar. The more interesting question is "where is the string data stored" - which is complex; the `string` type is an exception to the usual rule, and the objects themselves have variable size; the character data is directly inside the `string` object (it is not the case that the `string` type has a pointer or reference to the character data; it is the character data, plus a length field).
Problem
I would like to know how the `String.Length` can count the characters number. If possible, a simple algorithm would be nice to explain it.