0
votes

in C#, What is the Complexity of StringBuilder.ToString()? Is it O(1), O(N), or something else?

1
Look at the source and you'll know everything you need: referencesource.microsoft.com/#mscorlib/system/text/…MarcinJuraszek

1 Answers

1
votes

It varies between framework version; in older versions StringBuilder works on a string directly, so there is no additional cost in .ToString(): it just hands you the data directly (which can mean oversized, but it makes it work); so O(1).

In newer framework version, it uses a char[] backing buffer, so now when you .ToString() it will probably need to copy 2 x Length bytes, making it O(N).