As per Reference Source the StringBuilder
class stores strings in a char Array.
Accessing this array via the property getter this[int index]
does a few checks and then returns the array item:
internal char[] m_ChunkChars; // The characters in this block
//...more stuff
[System.Runtime.CompilerServices.IndexerName("Chars")]
public char this[int index] {
//
get {
StringBuilder chunk = this;
for (; ; )
{
int indexInBlock = index - chunk.m_ChunkOffset;
if (indexInBlock >= 0)
{
if (indexInBlock >= chunk.m_ChunkLength)
throw new IndexOutOfRangeException();
return chunk.m_ChunkChars[indexInBlock];
}
chunk = chunk.m_ChunkPrevious;
if (chunk == null)
throw new IndexOutOfRangeException();
}
}
//... more stuff
}
Thus the complexity is O(1) or constant access time.