-3

Instead of

string someString = "AB";

I stumbled upon

string someString = "A" + "B";

Is there any technical explanation which renders the second way more advantageous?

Ruudjah
  • 807
  • 7
  • 27
  • 4
    The compiler will compile both to identical IL, so there is no difference. – Michael Liu Feb 03 '15 at 15:01
  • There is no difference because the compiler can do a lot of optimisation of literal strings in code. My best guess is this code used to read something like `string someString = "A" + integerValue + "B";` and was subsequently edited to remove the non-literal part, and they did a lazy job of it. – Adam Houldsworth Feb 03 '15 at 15:03
  • Does this question belong on SO? Where is the specific problem? –  Feb 03 '15 at 15:12
  • Note that this topic is already covered couple times on SO - like http://stackoverflow.com/questions/5164436/string-concat-using-constants-performance (somewhat different comparison, but answers this question too) – Alexei Levenkov Feb 03 '15 at 15:18

1 Answers1

5
var answer = "The only thing advantageous is readability, if you have large " +
             "amounts of text it can be useful to break up the text inside the " +
             "source code for better readability. The compiler will turn it " +
             "in to a single string at compile time anyway";
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • The @"verbatim strings" are even more readable. They can include newlines without any fuss. – H H Feb 03 '15 at 15:04
  • 4
    @HenkHolterman But if you don't *want* the rendered string to have those newlines, then it's a problem. – Servy Feb 03 '15 at 15:05
  • 1
    Meh, a string that long should be a localised resource somewhere, but now I'm just trolling ;-) – Adam Houldsworth Feb 03 '15 at 15:06
  • Doesn't really apply to any C# standard that I can think of, but also one of Python's main code standard (Pep8) likes you to make your lines of code only less than a certain length (80 chars I believe) – Sayse Feb 03 '15 at 15:06
  • @AdamHouldsworth actually that is not a troll at all but good advice. Had this been a real program I would have made that string a resource too. – Scott Chamberlain Feb 03 '15 at 15:08
  • @ScottChamberlain Yeah, it's half and half. We localise all our display strings by default because You Never Know(tm), but localising it purely because it's long is the troll bit. We don't localise our exception messages, for example. – Adam Houldsworth Feb 03 '15 at 15:09