Will the compiler optimize pretty formatted strings, or will that code run slower than strings that are not divided in a readable way?
for example
string sql =
"select * " +
"from person " +
"where id = :id";
or
string sql = "select * from person where id = :id";
This is just a small example. You know how complicated the sql can get.
Just use:-
string sql =
@"select *
from person
where id = :id";
This is from the compilers point of view identical to the single line solution. Although I wouldn't be surprised to see the concatenation of literal strings optimised out by the compiler anyway. However the common gotcha with the concatenation approach is forgetting to include whitespace at the end of string.
You can test this with a simple program:
Console.WriteLine("a" + "b");
Using reflector, you can easily disassemble the resulting binary. In Release mode, the IL this generates is:
L_0000: ldstr "ab"
L_0005: call void [mscorlib]System.Console::WriteLine(string)
So .NET does optimize "pretty formatted strings".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With