string.Fromat vs StringBuilder vs +

gustierng
gustierng used Ask the Experts™
on
Which is more efficient:

1. string.Fromat("some text {0}", text)
2. Stringbuilder
3. concatinate using +
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Stringbuilder is most efficient then String.format and finally +

Why is stringbuilder more efficient than string.format? Its because string.format actually uses stringBuilder internally and the applies the formatting.

+ is the slowest as string is immutable, which means you have to create a new string object each time you change the value of a string variable

Anurag ThakurTechnical Manager

Commented:
+ will be the slowest operation and mentioned by amx and to further add on to what amx has to say please go through the following link
http://www.dotnetjunkies.com/WebLog/tituswave/archive/2005/04/09/63241.aspx
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
The comparison of methods depends on the situation.  It would be inaccurate to make such generalizations as "StringBuilder is fastest" and "+ operator is slowest".

When you need to build a long string with multiple operations, this is certainly true.  If the desire is siimply to concatenate two existing strings, the + operator will be just as good, and potentially better.  If the desired result is something that can be accomplished by a single call to String.Format, then that will be just as efficient as Stringbuilder.  Since the Format method tries to make a guess as to the length of your result whereas the average programmer uses default construction, Format may actually outperform Stringbuilder.

Bottom line is that you need to take into account your specific programming need before you can say which method is going to be most efficient.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial