views:

3598

answers:

9

I am wondering what is the "best practice" to break long strings in C# source code. Is this string

"string1"+
"string2"+
"string3"

concatenated during compiling or in run time?

+3  A: 

The concatenation is done at compile time, so there is no runtime overhead.

+17  A: 

It's done at compile time. That's exactly equivalent to "string1string2string3".

Suppose you have:

string x = "string1string2string3"
string y = "string1" + "string2" + "string3"

The compiler will perform appropriate interning such that x and y refer to the same objects.

EDIT: There's a lot of talk about StringBuilder in the answers and comments. Many developers seem to believe that string concatenation should always be done with StringBuilder. That's an overgeneralisation - it's worth understanding why StringBuilder is good in some situations, and not in others.

Jon Skeet
A: 

Can't you use StringBuilder?

Damien
Doing so would make the code less readable *and* less performant. In other words, it would be a bad thing to do.
Jon Skeet
Fair enough, I thought that if you were doing a lot of concatenation then stringbuilder was recommended?
Damien
If you are doing lots of concatenation during runtime, then yes, StringBuilder is recommended. But the example above has static strings, so the compiler already optimizes it down to a single string.
And even if the concatenation is done at runtime, concatenating two strings with "+" is faster than using StringBuilder.
Jon Skeet
Well, I did say "lots" =) But you're right, I should have qualified that further.
+6  A: 

Your example will be concatenated at compile time. All inline strings and const string variables are concatenated at compile time.

Something to keep in mind is that including any readonly strings will delay concatting to runtime. string.Empty and Environment.NewLine are both readonly string variables.

James Newton-King
A: 

it really depends on what you need. Generally, if you need to concat strings, the best performance in runtime will be achieved by using StringBuilder. If you're referring in source code something like var str = "String1"+"String2" it will be converter into string str = "String1String2" on compilation. In this case you have no concatenation overhead

Tamir
Your generalisation is too general - if you can perform the whole concatenation in one go, it's usually faster (and more readable) than using StringBuilder. So prefer "x + y + z" to new StringBuilder(x).Append(y).Append(z).ToString(). StringBuilder is useful when there are *repeated* concatenations.
Jon Skeet
To second Jon's point... StringBuilder is useful when looping etc. For a single set of operations, string.Concat is simpler and works the same.
Marc Gravell
Thanks Marc - I'd run out of space :) Edited my answer to point to my StringBuilder article though...
Jon Skeet
+ operator is actually calls concat method. If you'll take a look into StringBuilder implementation, it's bit smarter :)
Tamir
+6  A: 

If the whitespace isn't important then you can use the @ escape character to write multi-line strings in your code. This is useful if you have a query in your code for example:

string query = @"SELECT whatever
FROM tableName
WHERE column = 1";

This will give you a string with line breaks and tabs, but for a query that doesn't matter.

Rune Grimstad
This was useful for me too! Thanks!
Alexander Prokofyev
+1  A: 

StringBuilder is a good way to go if you have many (more than about four) strings to concatenate. It's faster.

Using String.Concat in you example above is done at compile time. Since they are literal strings they are optimized by the compiler.

If you however use variables:

string a = "string1";
string b = "string2";
string c = a + b;

This is done at runtime.

Sani Huttunen
The "more than 4" is only relevant when looping over a set of data; otherwise, a single call to string.Concat (passing an array if necessary) is identical.
Marc Gravell
What you're saying is true.http://dotnetperls.com/Content/StringBuilder-Performance.aspx
Sani Huttunen
A: 

StringBuilder will be your fastest approach if you are using any amount of strings.

http://dotnetperls.com/Content/StringBuilder-1.aspx

If you are just doing a few string (5 or less is a good rule) the speed will not matter of what kind of concatenation you are using.

naspinski
String builder is the faster *runtime* solution, but the expression given would be better evaluated at compile time (which i think shouldn't be a problem for the compiler)
peterchen
A: 

Hi Guys!! There´s any way to do it. My favorete uses a string´s method´s from C#. Sample One:

string s=string.Format("{0} {1} {0}","Hello","By"); result is s="Hello By Hello";