By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,649 Members | 1,808 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,649 IT Pros & Developers. It's quick & easy.

String concatenation -- not the typical question

P: n/a
Hi,

Please read carefully before assuming that this is the same old
question about string concatenation in C#!

It is well-known that the following concatenation produces multiple
immutable String objects for each statement:

String a = "a";
a += "b";
a += "c";
a += "d";

However, I have not seen any source say whether or not the following
statement creates a new String object for each concatenation (i.e.,
each "+" sign):

String b = a + "b" + "c" + "d";

In other words: if the concatenation is done all in one statement, does
the compiler optimize the assignment to b? If the assignment to b had,
say, twenty such concatenations in it, would there be a reason to use
StringBuilder?

Thanks very much.

Justin

Mar 18 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Two more questions:
What if the terms of the assignment to b were all string literals?
What if the terms of the assignment to b were all string variables?

Thanks!
Justin

Mar 18 '06 #2

P: n/a

"Justin M. Keyes" <ju******@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
<snip>
However, I have not seen any source say whether or not the following
statement creates a new String object for each concatenation (i.e.,
each "+" sign):

String b = a + "b" + "c" + "d";
This statement in simply converted into concatenation behind the scenes.
<Unverified>
It is also possible that it will optimize away the concatenation of literals altogether.
</Unverified>

In other words: if the concatenation is done all in one statement, does
the compiler optimize the assignment to b?
Yes
If the assignment to b had,
say, twenty such concatenations in it, would there be a reason to use
StringBuilder?


If you are using + as opposed to += you are fine doing it this way
Mar 18 '06 #3

P: n/a
Thanks for your help. For literals I certainly agree, but what if the
statement uses variables? E.G., my statement above has the variable 'a'
as a term.

Mar 18 '06 #4

P: n/a
The compiler is typically pretty smart about these things. Take a look at
your code using reflector:

static void Main(string[] args)
{
string a = "A" + "B" + "C" + "D" + "E" + "F" + "G";
Console.WriteLine(a);
}

private static void Main(string[] args)
{
string text1 = "ABCDEFG";
Console.WriteLine(text1);
}

but it not smarter than your application:

static void Main(string[] args)
{
string a = "A";
string b = "B";
string c = "C";
string d = a + b + c;
Console.WriteLine(a);
}

private static void Main(string[] args)
{
string text1 = "A";
string text2 = "B";
string text3 = "C";
string text4 = text1 + text2 + text3;
Console.WriteLine(text1);
}
and now for a slightly different question and answer:

static void Main(string[] args)
{
string a = "A" + "B" + "C" + "D" + "E" + "F" + "G";
string b = "ABCDEFG";
}

take a look at the debug disassembly and you will see that both a and b are
initialized with the same literal:

string a = "A" + "B" + "C" + "D" + "E" + "F" + "G";
00000029 mov eax,dword ptr ds:[0227307Ch]
0000002f mov edi,eax
string b = "ABCDEFG";
00000031 mov eax,dword ptr ds:[0227307Ch]
00000037 mov esi,eax


--
Andrew Robinson
http://blog.binaryocean.com
"Justin M. Keyes" <ju******@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Two more questions:
What if the terms of the assignment to b were all string literals?
What if the terms of the assignment to b were all string variables?

Thanks!
Justin

Mar 18 '06 #5

P: n/a
Bill Butler <qw****@asdf.com> wrote:

"Justin M. Keyes" <ju******@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
<snip>
However, I have not seen any source say whether or not the following
statement creates a new String object for each concatenation (i.e.,
each "+" sign):

String b = a + "b" + "c" + "d";


This statement in simply converted into concatenation behind the scenes.
<Unverified>
It is also possible that it will optimize away the concatenation of
literals altogether.
</Unverified>


Not just possible - guaranteed. Whenever there is a constant expression
(as per the spec definition of "constant expression") the expression is
evaluated at compile-time.

--
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
Mar 18 '06 #6

P: n/a
Justin M. Keyes <ju******@gmail.com> wrote:
Thanks for your help. For literals I certainly agree, but what if the
statement uses variables? E.G., my statement above has the variable 'a'
as a term.


It ends up as a single concatenation: a+"bcd". That's as fast as (or
faster than) using a StringBuilder.

--
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
Mar 18 '06 #7

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bill Butler <qw****@asdf.com> wrote:

"Justin M. Keyes" <ju******@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
<snip>
> However, I have not seen any source say whether or not the following
> statement creates a new String object for each concatenation (i.e.,
> each "+" sign):
>
> String b = a + "b" + "c" + "d";


This statement in simply converted into concatenation behind the scenes.
<Unverified>
It is also possible that it will optimize away the concatenation of
literals altogether.
</Unverified>


Not just possible - guaranteed. Whenever there is a constant expression
(as per the spec definition of "constant expression") the expression is
evaluated at compile-time.


Thanks Jon,

I wasn't ABSOLUTLY sure so I waffled.
Mar 18 '06 #8

P: n/a
I'm surprised that Jon didn't point you to his page on StringBuilder :)
Here it is.

http://www.yoda.arachsys.com/csharp/stringbuilder.html

The answer is that if the statement uses variables, it is compiled into
a single call to String.Concat(), which does not create intermediate
copies of the string. You get multiple intermediate copies only if you
write this:

string a = b;
a += c;
a += d;
a += e;
....

Mar 18 '06 #9

P: n/a
That is *exactly* what I was looking for.

Bruce Wood, Andrew Robinson, Bill Butler, and Jon Skeet, thanks very
much!

Apr 3 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.