2

Possible Duplicate:
String vs StringBuilder

i know .NET Strings are immutable which is the reason why a new string object is created every time we alter it (insert, append, remove, etc.).

That sounds reasonable, so why do we still use the .NET String class functions and not the faster StringBuilder?

Community
  • 1
  • 1
Milan Mendpara
  • 3,091
  • 4
  • 40
  • 60
  • Regarding the last part of your question, `strings`, as you say, are immutable. `StringBuilders` aren't (that's their primary asset). Both types allow you to work with characters, but they don't have the same semantics at all, and one cannot completely replace the other. – Frédéric Hamidi Jan 20 '12 at 20:46
  • 2
    This [String vs StringBuilder question](http://stackoverflow.com/questions/73883/string-vs-stringbuilder) has 32 votes and 24 answers. – DOK Jan 20 '12 at 20:48

3 Answers3

4

StringBuilder is faster for repeated (non-compile-time) concatenations in a loop.

For ordinary string operations, they perform equivalently.

In particular, a + b + c is compiled as String.Concat(a, b, c), which allocates a single buffer and is as fast or faster than a string builder.
StringBuilder is only faster than multiple separate Concats, since each Concat will need to make a new buffer.

Ry-
  • 218,210
  • 55
  • 464
  • 476
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Simple: it's easier, and performance is rarely an issue. Maintainability is much more important unless specifically otherwise required by the project.

Which is easier to you? This:

public override string ToString() {
    return this.LastName + ", " + this.FirstName;
}

or this?

public override string ToString() {
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append(this.LastName);
    sb.Append(", ");
    sb.Append(this.FirstName);
    return sb.ToString();
}

Admittedly, you'd probably use var and using System.Text;, but it's still much more complicated and much less readable. And StringBuilder is mostly for concatenation in some form of loop, since otherwise the operations can be optimized, so you rarely need to use them.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 2
    As an alternative, return String.Format("{0}, {1}", Lastname, Firstname); which creates one string instead of 2 (of which one is thrown away right away) – Joachim Isaksson Jan 20 '12 at 20:46
  • @JoachimIsaksson: Sure, that too :) – Ry- Jan 20 '12 at 20:46
  • 2
    Actually, in this case the first method will probably perform better too. StringBuilder is useful when you *can't* express the whole concatenation in a single expression. – Jon Skeet Jan 20 '12 at 20:47
  • Or of course, sb.AppendFormat("{0}, {1}", Lastname, Firstname); – fordareh Jan 20 '12 at 20:49
  • You can also chain the sb calls, since the sb operations return sb. `sb.Append(this.LastName).Append(", ").Append(this.FirstName); – Olivier Jacot-Descombes Jan 20 '12 at 21:02
  • 1
    @JoachimIsaksson `LastName + ", " + FirstName` is compiled as `string.Concat(LastName, ", ", FirstName)` so it doesn't actually create an extra string. `string.Format()`, on the other hand, has a lot of overhead that `string.Concat()` doesn't. So `LastName + ", " + FirstName` really is more efficient. – phoog Jan 20 '12 at 21:12
  • @phoog Cool, I had to idlasm to confirm that, you learn something new every day :) @ minitech Definitely no downvote from me. – Joachim Isaksson Jan 21 '12 at 01:03
  • @JoachimIsaksson There are some good pieces about this online. Jon Skeet has a very good one, though it is slightly out of date as it discusses some implementation details of StringBuilder, and that implementation changed quite a bit in .NET 4. – phoog Jan 21 '12 at 04:27
0

Looking at the MSDN articles for both String and StringBuilder there are several methods on String that are not on StringBuilder. Trim, IndexOf, Pad[Left|Right], ToUpper, and ToLower just to name a few. On top of that as minitech pointed out, StringBuilder is a pain to work with. StringBuilder is great if you have a lot of string manipulations to do. But for the simple stuff, String works just fine.

cadrell0
  • 17,109
  • 5
  • 51
  • 69