vpiTriumph
vpiTriumph

Reputation: 3166

Are string concatenations using + optimized to a StringBuilder implementation in .NET?

I've read multiple places that in Java 1.5+ String concatenations are optimized to using a StringBuilder when a program is compiled. It's unclear to me if this is a standard or just a common optimization many compilers employ. Any clarificion in regards to this would be appreciated, but mainly it's a lead-in to my second question.

Does .NET similarly optimize? I'm aware that if I use StringBuilder this will eliminate any ambiguity but I personally find the simplicity of + easier to read. If .NET does, did this start in a specific version? Elaboration is appreciated.

Upvotes: 2

Views: 715

Answers (5)

supercat
supercat

Reputation: 81123

If you concatenate multiple strings in one operation with String.Concat, .net will add together their lengths, create a new string object of suitable size, and copy all of the characters from the source strings to the new one. If you are doing four or fewer strings in a String.Concat call, one new object will be created (the new string); no "garbage" will be created unless one of the old strings is abandoned and thus becomes garbage. If you concatenate five or more strings, a new temporary String[] will be created to hold the parameters, but it will be small and short-lived (and thus relatively harmless), regardless of the length of the strings, unless you're passing many thousands of parameters.

Additionally, one can concatenate any number of constant strings using the + operator in C#, or the + or & operators in vb.net and the compiler will turn them into one larger constant string. I'm not sure what rules dictate the extent to which the C# or vb.net compiler will consolidate strings in an expression which contains multiple constant and non-constant strings (especially if parentheses are involved); using a String.Concat call with as many parameters as needed, but using + to consolidate constant strings within a parameter, should yield optimal code in any case.

Upvotes: 0

walther
walther

Reputation: 13600

No, AFAIK .NET doesn't do this automatically, you need to be specific. Using + creates every time a new string, because type string is immutable.

I'd suggest reading this for example: Most efficient way to concatenate strings?

Hint - accepted answer may not be always the correct one.

And this : http://en.csharp-online.net/Manipulating_Strings_in_CSharp%E2%80%94Concatenating_Strings

Upvotes: 0

HatSoft
HatSoft

Reputation: 11191

Hope this will give you a better view towards StringBuilder then string conctenation with +

Performance Considerations

A String object concatenation operation always creates a new object from the existing string and the new data. A StringBuilder object maintains a buffer to accommodate the concatenation of new data. New data is appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, and the new data is appended to the new buffer.

The performance of a concatenation operation for a String or StringBuilder object depends on the frequency of memory allocations. A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation allocates memory only if the StringBuilder object buffer is too small to accommodate the new data. Use the String class if you are concatenating a fixed number of String objects. In that case, the compiler may even combine individual concatenation operations into a single operation. Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you're using a loop to concatenate a random number of strings of user input.

MSDN

Upvotes: 1

Ant P
Ant P

Reputation: 25221

From MSDN:

A String object concatenation operation always creates a new object from the existing string and the new data. A StringBuilder object maintains a buffer to accommodate the concatenation of new data. New data is appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, and the new data is appended to the new buffer.

The performance of a concatenation operation for a String or StringBuilder object depends on the frequency of memory allocations. A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation allocates memory only if the StringBuilder object buffer is too small to accommodate the new data. Use the String class if you are concatenating a fixed number of String objects. In that case, the compiler may even combine individual concatenation operations into a single operation. Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you're using a loop to concatenate a random number of strings of user input.

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

Upvotes: 4

Mehmet Ali Sert
Mehmet Ali Sert

Reputation: 184

No, + operator isn't optimized with StringBuilder in .NET . In MSDN, there is a mention like, if you want to speed up string operation, you can use StringBuilder

Upvotes: 0

Related Questions