Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenation with StringBuilder

Tags:

c#

.net

vb.net

Is it from a sight of resources reasonable to use a StringBuilder already to concat two strings or is there a minimum concatenation operations that makes the StringBuilder efficient?

like image 330
Heinz K Avatar asked Jul 09 '10 21:07

Heinz K


3 Answers

...the String class is preferable for a concatenation operation if a fixed number of String objects are concatenated

like image 137
Yuriy Faktorovich Avatar answered Nov 05 '22 00:11

Yuriy Faktorovich


String.Join is the fastest one as it allocates all the required memory in one operation. See this article: StringBuilder vs. String / Fast String Operations with .NET 2.0

like image 42
Giorgi Avatar answered Nov 05 '22 00:11

Giorgi


Concatenating two strings with StringBuilder won't give you any benefits, since the result still has to be converted to a string - so concatenating them directly is one allocation + two copies. StringBuilder can't do any better - only clutter your code.

like image 6
EFraim Avatar answered Nov 05 '22 01:11

EFraim