469,364 Members | 2,332 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

StringBuilder.Append() vs concatenation

Hi folks,

I'm aware of the fact that using a StringBuilder is more efficient than
constantly reassigning a string. For example.

Example A
---------
SringBuilder sb = new StringBuilder();
sb.Append("Some text");
sb.Append(" and some more text");

is more efficient than

Example B
---------
string myString = "Some text";
myString += " some more text";

But what about this.

Example C
---------
StringBuilder sb = new StringBuilder();
sb.Append("Variable = " + myVar + " and that's that.");

vs.

Example D
---------
StringBuilder sb = new StringBuilder();
sb.Append("Variable = ");
sb.Append(myVar);
sb.Append(" and that's that.");

I'm thinking that the concatenation in the Example C does not have the
same overhead as the reassignment in Example B and therefore Example D
is overkill, but I'm prepared to be corrected.

Cheers

Simon
Dec 3 '05 #1
6 5907
sb.Append("Variable = " + myVar + " and that's that.");
has concatenation the operation inside brackets is the same as Example B

so the most efficient will be Example D.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Simon" <si****************@nodomain.com> wrote in message
news:ev****************@TK2MSFTNGP10.phx.gbl...
Hi folks,

I'm aware of the fact that using a StringBuilder is more efficient than
constantly reassigning a string. For example.

Example A
---------
SringBuilder sb = new StringBuilder();
sb.Append("Some text");
sb.Append(" and some more text");

is more efficient than

Example B
---------
string myString = "Some text";
myString += " some more text";

But what about this.

Example C
---------
StringBuilder sb = new StringBuilder();
sb.Append("Variable = " + myVar + " and that's that.");

vs.

Example D
---------
StringBuilder sb = new StringBuilder();
sb.Append("Variable = ");
sb.Append(myVar);
sb.Append(" and that's that.");

I'm thinking that the concatenation in the Example C does not have the
same overhead as the reassignment in Example B and therefore Example D is
overkill, but I'm prepared to be corrected.

Cheers

Simon

Dec 3 '05 #2
Simon <si****************@nodomain.com> wrote:
I'm aware of the fact that using a StringBuilder is more efficient than
constantly reassigning a string. For example.


<snip>

See http://www.pobox.com/~skeet/csharp/stringbuilder.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 3 '05 #3
Ok,

I remember reading some time ago that

var = "This" + " is" + " a" + " test";

does not have same issues as

var = "This";
var += " is";
var += " a";
var += " test";

Hence my question. I can see where your coming from on a conceptual
level and how it would reduce down to the same tokens, just confused as
I had read the contrary before.

I take it that this was wrong.

Cheers

Simon
Vadym Stetsyak wrote:
sb.Append("Variable = " + myVar + " and that's that.");
has concatenation the operation inside brackets is the same as Example B

so the most efficient will be Example D.

Dec 3 '05 #4
Thanks Jon,

Very clear article.

Many thanks

Simon

Jon Skeet [C# MVP] wrote:
Simon <si****************@nodomain.com> wrote:
I'm aware of the fact that using a StringBuilder is more efficient than
constantly reassigning a string. For example.

<snip>

See http://www.pobox.com/~skeet/csharp/stringbuilder.html

Dec 3 '05 #5
Simon <si****************@nodomain.com> wrote:
I remember reading some time ago that

var = "This" + " is" + " a" + " test";

does not have same issues as

var = "This";
var += " is";
var += " a";
var += " test";


Well, there are two issues there.

1) The first is actually a constant, so it becomes exactly equivalent
to:

var = "This is a test";

2) Even with variables, it would be one concatenation call, without
intermediate strings being constructed.

See http://www.pobox.com/~skeet/csharp/stringbuilder.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 3 '05 #6
Yup I get ya Jon. Not the best example of what I was thinking. I've read
the article. All is clear.

Many thanks

Simon

Jon Skeet [C# MVP] wrote:
Simon <si****************@nodomain.com> wrote:
I remember reading some time ago that

var = "This" + " is" + " a" + " test";

does not have same issues as

var = "This";
var += " is";
var += " a";
var += " test";

Well, there are two issues there.

1) The first is actually a constant, so it becomes exactly equivalent
to:

var = "This is a test";

2) Even with variables, it would be one concatenation call, without
intermediate strings being constructed.

See http://www.pobox.com/~skeet/csharp/stringbuilder.html for more
information.

Dec 4 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Alvin Bruney | last post: by
2 posts views Thread by Ron | last post: by
12 posts views Thread by Richard Lewis Haggard | last post: by
10 posts views Thread by =?Utf-8?B?RGFyYSBQ?= | last post: by
5 posts views Thread by pantagruel | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.