Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

question from:https://stackoverflow.com/questions/283476/best-way-to-break-long-strings-in-c-sharp-source-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
381 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...