Since strings are immutable in .NET, why are they copied for simple operations such as Substring
or Split
? For example, by keeping a char[] value
, int start
and int length
, a substring could be created to simply point to an existing string, and we could save the overhead of copying the string for many simple operations. So I wonder, why was the decision chosen to copy strings for such operations?
For example, was this done to support the current implementation of StringBuilder
? Or to avoid keeping a reference to a large char[]
when only a few characters are required? Or any other reason you can think of? Can you suggest pros and cons for such design?
As mentioned by @cletus and supported by @Jon Skeet, this is more like asking why .NET strings were built differently from Java in this aspect.