string.Fromat vs StringBuilder vs +

Which is more efficient:

1. string.Fromat("some text {0}", text)
2. Stringbuilder
3. concatinate using +
gustierngAsked:
Who is Participating?
 
amxConnect With a Mentor 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

0
 
Anurag ThakurTechnical ManagerCommented:
+ 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
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
drichardsCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.